/// <summary> /// 获取缓存的 /// </summary> /// <param name="key"></param> /// <returns></returns> private static EndpointAddress GetAddress(string contract, string uri) { string server_name = GetServerName(contract); WcfClentBinding wcfcbing = GetWcfClientConst(server_name); WcfClent client = GetContract(contract); string key = uri == null ? contract : uri + "/" + contract; EndpointAddress address = null; if (endpointDic.ContainsKey(key)) { address = endpointDic[key]; } else { string Uri = uri == null ? wcfcbing.Uri : uri; string baseaddress = Uri.EndsWith("/") ? Uri.TrimEnd('/') : Uri; string extendaddress = client.Address.StartsWith("/") ? client.Address.TrimStart('/') : client.Address; address = new EndpointAddress("net.tcp://" + baseaddress + "/" + extendaddress); //缓存终结点 if (!endpointDic.ContainsKey(contract)) { lock (lockpoint) { if (!endpointDic.ContainsKey(key)) { endpointDic.Add(key, address); } } } } return(address); }
/// <summary> /// 得到负载的通道工厂 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="uri"></param> /// <returns></returns> public static ChannelFactory <T> GetFactory <T>(Address uri_address, bool EnableBinaryFormatterBehavior) { string contract = typeof(T).FullName; string key = uri_address.Uri + "/" + contract; ChannelFactory <T> client = null; if (factorycache.ContainsKey(key)) { client = (ChannelFactory <T>)factorycache[key]; if (client.State != CommunicationState.Opened) { client = null; } } if (client == null) { NetTcpBinding binging = GetBing(contract); EndpointAddress address = GetAddress(contract, uri_address.Uri); WcfClent client_contract = GetContract(contract); client = new ChannelFactory <T>(binging, address); //增加头信息行为 ClientMessageInspector msgBehavior = new ClientMessageInspector(); client.Endpoint.Behaviors.Add(msgBehavior); //如果启用自定义二进制序列化器 if (EnableBinaryFormatterBehavior) { if (client.Endpoint.Behaviors.Find <BinaryFormatterBehavior>() == null) { BinaryFormatterBehavior serializeBehavior = new BinaryFormatterBehavior(); client.Endpoint.Behaviors.Add(serializeBehavior); } } //如果有MaxItemsInObjectGraph配置指定配置此行为 if (client_contract.MaxItemsInObjectGraph != null) { foreach (OperationDescription op in client.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find <DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = (int)client_contract.MaxItemsInObjectGraph; } } } factorycache[contract] = client; } return(client); }
/// <summary> /// 构造函数 /// </summary> /// <param name="Binging"></param> /// <param name="Address"></param> public WcfStandardProxy(NetTcpBinding Binging, EndpointAddress Address, WcfClentBinding wcfcbing, WcfClent Client, string Server_Name, bool IsUseWcfPool) : base(typeof(T)) { this.binging = Binging; this.address = Address; this.EnableBinaryFormatterBehavior = wcfcbing.EnableBinaryFormatterBehavior; this.wcfMaxPoolSize = wcfcbing.WcfMaxPoolSize; this.wcfOutTime = wcfcbing.WcfOutTime; this.WcfFailureTime = wcfcbing.WcfFailureTime; this.client = Client; this.server_name = Server_Name; this.isUseWcfPool = IsUseWcfPool; //初始话连接池 WcfPoolCache.Init(this.isUseWcfPool, wcfMaxPoolSize, wcfOutTime, WcfFailureTime, server_name, wcfcbing.WcfPoolMonitorReapTime); }
/// <summary> /// 获取缓存的常量配置 /// </summary> /// <param name="contract">契约名</param> /// <param name="binging">绑定信息</param> /// <param name="address">终结点地址</param> /// <param name="client">契约配置</param> /// <param name="wcfcbing">客户端常量配置</param> /// <param name="server_name">服务器名</param> public static void GetCachedData(string contract, out NetTcpBinding binging, out EndpointAddress address, out WcfClent client, out WcfClentBinding wcfcbing, out string server_name) { //获取常量 server_name = GetServerName(contract); wcfcbing = GetWcfClientConst(server_name); client = GetContract(contract); //获取绑定 binging = GetBing(contract); //获取终结点,注:没启用负载均衡,缓存终结点的的key是契约,启用负载均衡,缓存终结点的key是address+"/"+contract address = GetAddress(contract, null); }