autofac 注入普通服务和WCF服务
Posted zslm___
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了autofac 注入普通服务和WCF服务相关的知识,希望对你有一定的参考价值。
using Autofac;
using Autofac.Builder;
using Autofac.Core;
//实现Autofac扩展
public static AutofacRegisterWcf
{
public static IRegistrationBuilder<TServiceContract, SimpleActivatorData, SingleRegistrationStyle> RegisterWcfServiceContract<TServiceContract>(this ContainerBuilder builder, string endpointConfigurationName = null)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
return Autofac.Integration.Wcf.RegistrationExtensions.UseWcfSafeRelease<TServiceContract, SimpleActivatorData, SingleRegistrationStyle>(builder.Register((IComponentContext _) => new ChannelFactory<TServiceContract>(endpointConfigurationName ?? typeof(TServiceContract).FullName).CreateChannel()).As<TServiceContract>());
}
}
在WebConfig里 定义 Wcf的 配置信息
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<bindings>
<customBinding>
<binding name="ServiceSoap12" closeTimeout="00:00:10" openTimeout="00:00:05" receiveTimeout="00:00:05" sendTimeout="00:00:05">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<!-- 短信网关服务 -->
<endpoint address="http://127.0.0.1:19922/SMS/SendMessage?wsdl" binding="customBinding" bindingConfiguration="ServiceSoap12" contract="G2.SendMessage.ISMSService" name="G2.SendMessage.ISMSService" />
</clien>
</system.serviceModel>
第三步 在项目启动时就可以注册Wcf服务了
WCF服务注入
public void RegisterWcf(Autofac.ContainerBuilder builder)
{
//SendMessage服务注入
builder.RegisterWcfServiceContract<G2.SendMessage.ISMSService>().InstancePerHttpRequest();
}
最后调用 Wcf服务
private readonly ISMSService _smsService;
_smsService = ContainerManager.Resolve<ISMSService>(); ;
以上是关于autofac 注入普通服务和WCF服务的主要内容,如果未能解决你的问题,请参考以下文章