Usage With Inversion of Control (IoC) Containers
ChannelAdam WCF Library — Version 2 Documentation
Usage With Inversion of Control (IoC) Containers
It is easy to use the ServiceConsumerFactory
from Inversion of Control (IoC) containers.
Below are examples with three popular IoC containers.
- AutoFac
- SimpleInjector
- Unity
Let’s assume that there is the following class and we want to perform constructor injection with it.
1public class ConstructorInjectedController
2{
3 public IServiceConsumer<IFakeService> FakeService { get; set; }
4
5 /// <summary>
6 /// Initializes a new instance of the <see cref="ConstructorInjectedController"/> class.
7 /// </summary>
8 /// <param name="fakeService">The fake service.</param>
9 public ConstructorInjectedController(IServiceConsumer<IFakeService> fakeService)
10 {
11 this.FakeService = fakeService;
12 }
13}
1. AutoFac
Here is how to use the ServiceConsumerFactory
with AutoFac.
1var builder = new Autofac.ContainerBuilder();
2builder.Register(c => ServiceConsumerFactory.Create<IFakeService>("BasicHttpBinding_IFakeService")).InstancePerDependency();
3builder.RegisterType<ConstructorInjectedController>();
4
5var autofacContainer = builder.Build();
6
7var controller = autofacContainer.Resolve<ConstructorInjectedController>();
2. SimpleInjector
Here is how to use the ServiceConsumerFactory
with SimpleInjector.
1var simpleInjectorContainer = new SimpleInjector.Container();
2simpleInjectorContainer.Register(() => ServiceConsumerFactory.Create<IFakeService>("BasicHttpBinding_IFakeService"), SimpleInjector.Lifestyle.Transient);
3
4var controller = simpleInjectorContainer.GetInstance<ConstructorInjectedController>();
3. Unity
And, here is how to use the ServiceConsumerFactory
with Unity.
1var unityContainer = new UnityContainer();
2
3unityContainer
4 .RegisterType<IServiceConsumer<IFakeService>>(
5 new TransientLifetimeManager(),
6 new InjectionFactory(c => ServiceConsumerFactory.Create<IFakeService>("BasicHttpBinding_IFakeService")))
7
8 //
9 // OR
10 //
11 //.RegisterType<IServiceConsumer<IFakeService>>(
12 // new InjectionFactory(c => ServiceConsumerFactory.Create<IFakeService>(() => new FakeServiceClient())))
13
14 //
15 // OR more 'correctly'
16 //
17 //.RegisterType<IFakeService, FakeServiceClient>(new TransientLifetimeManager())
18 //.RegisterType<IServiceConsumer<IFakeService>>(
19 // new InjectionFactory(c =>
20 // ServiceConsumerFactory.Create<IFakeService>(() => (ICommunicationObject)c.Resolve<IFakeService>())))
21 ;
22
23var controller = unityContainer.Resolve<ConstructorInjectedController>();
Automatic Type Registration
If you decide to automatically register types from all available assemblies, ensure that you exclude the types from the ChannelAdam WCF assembly.
For instance, with Unity, note the Where
:
1container.RegisterTypes(
2 AllClasses.FromLoadedAssemblies().Where(t => t.Namespace != typeof(IServiceConsumer<>).Namespace),
3 WithMappings.FromMatchingInterface,
4 WithName.Default);
If you do not exclude the ChannelAdam.Wcf assembly, the container will likely not be able to automatically resolve IRetryPolicyFunction. If you then register IRetryPolicyFunction manually, the container will continue down a rabbit hole and not be able to resolve System.ServiceModel.ICommunicationObject. Best to not attempt to register types in the ChannelAdam.Wcf library in the first place!