问题
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