我们可以在wcf服务中使用简单的注入器进行依赖注入吗?
我们可以在wcf服务中使用simple injector进行依赖注入,下面是我的演示,安装完这个NuGet包后,必须在应用程序的启动路径中调用SimpleInjectorServiceHostFactory.SetContainer方法进行初始化。
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);
}
对于每个服务类,你应该在每个服务类的.SVC文件中提供一个工厂属性。例如
<%@ ServiceHost
Service="Demo_rest_IIS.Service1"
CodeBehind="Service1.svc.cs"
Factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,
SimpleInjector.Integration.Wcf"%>
通过构造方法注入服务。
public class Service1 : IService1
{
private IDemo demo;
public Service1(IDemo demo){
this.demo = demo;
}
}
你可以参考下面的链接。
https:/github.comsimpleinjectorDocumentationblobmastersourcewcfintegration.rst。