zoukankan      html  css  js  c++  java
  • WCF

      /// <summary>
        /// IOC实例提供者,基于AutoFac
        /// 
        /// </summary>
        public class IocInstanceProvider : IInstanceProvider
        {
            Type serviceType;
            IContainer container;
    
            public IocInstanceProvider(Type serviceType)
            {
                this.serviceType = serviceType;
                container = RegisterDependency();
            }
            private IContainer RegisterDependency()
            {
                var builder = new ContainerBuilder();
    
               
                builder.RegisterType(Service1).InstancePerLifetimeScope();
                
    
                //配置外部接口
               builder.RegisterType<Test>().As<ITest>().SingleInstance();
                return builder.Build();
            }
    
    
            public object GetInstance(InstanceContext instanceContext, Message message)
            {
                return container.Resolve(serviceType);
            }
    
            public object GetInstance(InstanceContext instanceContext)
            {
                return GetInstance(instanceContext, null);
            }
    
            public void ReleaseInstance(InstanceContext instanceContext, object instance)
            {
                if (instance is IDisposable)
                    ((IDisposable)instance).Dispose();
            }
        }
    /// <summary>
        /// 使用IOC必须加上的特性,只能在实现类上使用
        /// 
        /// </summary>
        public class IocServiceBehavior : Attribute, IServiceBehavior
        {
            public void AddBindingParameters(ServiceDescription serviceDescription
                                            , ServiceHostBase serviceHostBase
                                            , Collection<ServiceEndpoint> endpoints
                                            , BindingParameterCollection bindingParameters)
            {
    
            }
    
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                foreach (ChannelDispatcher item in serviceHostBase.ChannelDispatchers)
                {
                    foreach (var ed in item.Endpoints)
                    {
                        if (!ed.IsSystemEndpoint)
                        {
                            ed.DispatchRuntime.InstanceProvider = new IocInstanceProvider(serviceDescription.ServiceType);
                        }
                    }
                }
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
            }
        }

    使用:

    [IocServiceBehavior]
    public class Service1:IService1{
    
      //methods
    }
  • 相关阅读:
    Salt-ssh批量自动安装被控端salt-mini
    Saltstack配置管理
    gitlab的安装和基本维护
    Git的杀手级功能之 一 远程仓库
    分布式版本控制系统-git
    Linux查看服务器公网ip的方法
    vmware fusion 10序列号
    python3.6.4的importlib模块重载用法
    设置PyCharm中的Python代码模版
    MacOs执行SQL出错(mysql)
  • 原文地址:https://www.cnblogs.com/gaobing/p/6831691.html
Copyright © 2011-2022 走看看