Search code examples
asp.net-mvcwcfsimple-injector

Simple Injector using WCF


I am new to Simple Injector. I have a WCF service with a class Testservice implementing a interface ITestService. As per the Simple Injector documentation, in the svc markup I have added the factory attribute as "SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory, SimpleInjector.Integration.Wcf". Also in the AppStart , I have registered the container using the following

container.RegisterWcfServices(Assembly.GetExecutingAssembly()); &
container.Register<ITestService, TestService>(Lifestyle.Scoped);

The WCF works fine. Now I need to consume the service TestService from my MVC app. In my MVC App, I have added the SimpleInjector.Integration.Web and SimpleInjector.Integration.Web.MVC through Nuget and also added the WCF service reference.

I am struck on registering the TestService class in App Start of my MVC application in order to inject in my controller. The container needs to be registered as

container.Register(ITestService, <TImplementation>);

but I am unable to resolve or find out what I need to give on the TImplementation. It requires a Implementation class which is the TestService but the TestService is available in WCF componebt and only I have the interface reference here in my MVC app.

Can somebody guide whether my approach is right and the solution for above. Thanks in Advance.


Solution

  • You can provide a factory method to SimpleInjector in the register method that it will use to build your object.

    container.Register<ITestService, TestService>(Lifestyle.Scoped);
    

    Can be written as

    container.Register<ITestService>(() => new TestServiceClient(), Lifestyle.Scoped);
    

    You are then free to choose the constructor you want to use in the factory method