[英]How to inject WCF Service Implementation into another WCF Service with Autofac
I have multiple WCF Services hosted in IIS and configured with Autofac. 我在IIS中托管了多个WCF服务,并使用Autofac进行配置。
Global.asax Global.asax中
var builder = new ContainerBuilder();
builder.RegisterType<ServiceA>();
builder.RegisterType<ServiceB>();
builder.RegisterType<ServiceC>();
var container = builder.Build();
AutofacHostFactory.Container = container;
web.config web.config中
<system.serviceModel>
...
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<serviceActivations>
<add service="ServiceA, MyServicesAssembly" relativeAddress="./ServiceA.svc" factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" />
<add service="ServiceB, MyServicesAssembly" relativeAddress="./ServiceB.svc" factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" />
<add service="ServiceC, MyServicesAssembly" relativeAddress="./ServiceC.svc" factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" />
</serviceActivations>
</serviceHostingEnvironment>
<system.serviceModel>
Service Implementations 服务实现
public class ServiceA : IServiceA
{
public ServiceA()
{
}
}
public class ServiceB : IServiceB
{
public ServiceB()
{
}
}
public class ServiceC : IServiceC
{
public ServiceC(IServiceA serviceA)
{
}
}
As you can see, ServiceC is different from the others and need an implementation of IServiceA. 如您所见,ServiceC与其他服务器不同,需要实现IServiceA。 Autofac can't resolve it, because there is no registration for IServiceA.
Autofac无法解决此问题,因为IServiceA没有注册。
So I change the registration to this: 所以我将注册更改为:
builder.RegisterType<ServiceA>().As<IServiceA>();
Autofac can now resolve ServiceC successfully, but the WCF hosting is not working anymore: Autofac现在可以成功解析ServiceC,但WCF托管不再有效:
An exception of type 'System.ServiceModel.ServiceActivationException' occurred in mscorlib.dll but was not handled in user code
mscorlib.dll中出现“System.ServiceModel.ServiceActivationException”类型的异常,但未在用户代码中处理
So my question is: 所以我的问题是:
Is there a way that I can have both, a hosted WCF Service instance, and the possibility to pass a Service Implementation to another Service? 有没有办法可以同时拥有一个托管的WCF服务实例,以及将服务实现传递给另一个服务的可能性? All configured with AutoFac?
全部配置了AutoFac? I'm also thinking of a workaround, but everything that comes to my mind results in a huge effort.
我也在想一个解决方法,但是我想到的一切都会带来巨大的努力。 I know that these services needs to be refactored, so that there is no need to pass in another "service".
我知道这些服务需要重构,因此不需要传递另一个“服务”。 But this is a different story.
但这是一个不同的故事。
If you want to expose a component as a set of services as well as using the default service, use the
AsSelf
method:如果要将组件公开为一组服务以及使用默认服务,请使用
AsSelf
方法:
//...
builder.RegisterType<ServiceA>()
.AsSelf() //<--
.As<IServiceA>();
//...
This will associate the class and interface together so that IServiceA
can be injected as needed and it will be resolved to ServiceA
. 这会将类和接口关联在一起,以便可以根据需要注入
IServiceA
,并将其解析为ServiceA
。 This also allow the WCF to not break as ServiceA
is registered. 这也允许WCF在
ServiceA
注册时不会中断。
Reference Autofac Documentation: Registration Concepts 参考Autofac文档:注册概念
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.