zoukankan      html  css  js  c++  java
  • 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>(); ;

  • 相关阅读:
    日记 2018/1/12
    【程序员笔试面试必会——排序①】Python实现 冒泡排序、选择排序、插入排序、归并排序、快速排序、堆排序、希尔排序
    Python笔试、面试 【必看】
    高性能Go并发
    Go连接MySql数据库Error 1040: Too many connections错误解决
    MAC 配置文件 ~/.zshrc
    go-statsd项目
    日记 2017.11.20
    sed 命令详解
    Opentsdb简介(一)
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5610094.html
Copyright © 2011-2022 走看看