码迷,mamicode.com
首页 > 其他好文 > 详细

autofac 注入普通服务和WCF服务

时间:2016-06-23 12:21:32      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

 

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服务

标签:

原文地址:http://www.cnblogs.com/zhshlimi/p/5610094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!