标签:
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>(); ;
标签:
原文地址:http://www.cnblogs.com/zhshlimi/p/5610094.html