Can we use simple injector for dependency injection in wcf services and How?

不问归期 提交于 2021-01-29 15:47:14

问题


Can we use simple injector for dependency injection in wcf services


回答1:


We can use simple injector for dependency injection in wcf services.Here is my demo. After installing this NuGet package, it must be initialized in the start-up path of the application by calling the SimpleInjectorServiceHostFactory.SetContainer method:

     protected void Application_Start(object sender, EventArgs e)
    {

        var container = new Container();
        container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

        container.RegisterWcfServices(Assembly.GetExecutingAssembly());


        container.Register<IDemo, Demo>();

        SimpleInjectorServiceHostFactory.SetContainer(container);
    }

For each service class, you should supply a factory attribute in the .SVC file of each service class. For instance:

    <%@ ServiceHost
Service="Demo_rest_IIS.Service1"
CodeBehind="Service1.svc.cs"
Factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,
    SimpleInjector.Integration.Wcf"%>

Inject service through construction method:

     public class Service1 : IService1
     {
       private IDemo demo;
       public Service1(IDemo demo){
        this.demo = demo;
        }
     }

You can refer to the link below:

https://github.com/simpleinjector/Documentation/blob/master/source/wcfintegration.rst



来源:https://stackoverflow.com/questions/61723357/can-we-use-simple-injector-for-dependency-injection-in-wcf-services-and-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!