• 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>(); ;

  • 相关阅读:
    Spring事务传播机制
    关于MyBatis-Like的模糊查询,">"、"<"等需转义字符描述
    MyBatis中if
    报错(持续.....)
    爬虫报错(持续.....)
    django的timezone问题
    dispatch
    django + uwsgi + nginx 实现高并发环境部署 及 报错处理
    虚拟机问题(持续更新.......)
    Tornado
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5610094.html
Copyright © 2020-2023  润新知