Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// host
        /// </summary>
        /// <param name="point"></param>
        /// <param name="debugbehavior"></param>
        /// <param name="throtbehavior"></param>
        /// <param name="bing"></param>
        private void OpenHost(ServicePoint point, ServiceDebugBehavior debugbehavior, ServiceThrottlingBehavior throtbehavior, NetTcpBinding bing, bool EnableBinaryFormatterBehavior)
        {
            ServiceHost host = new ServiceHost(point.Name, new Uri("net.tcp://" + constantSetting.BaseAddress));

            #region behavior
            if (host.Description.Behaviors.Find <ServiceDebugBehavior>() != null)
            {
                host.Description.Behaviors.Remove <ServiceDebugBehavior>();
            }
            if (host.Description.Behaviors.Find <ServiceThrottlingBehavior>() != null)
            {
                host.Description.Behaviors.Remove <ServiceThrottlingBehavior>();
            }

            host.Description.Behaviors.Add(debugbehavior);
            host.Description.Behaviors.Add(throtbehavior);

            //大数据量传输时必须设定此参数
            if (point.MaxItemsInObjectGraph != null)
            {
                if (host.Description.Behaviors.Find <DataContractSerializerOperationBehavior>() != null)
                {
                    host.Description.Behaviors.Remove <DataContractSerializerOperationBehavior>();
                }
                //通过反射指定MaxItemsInObjectGraph属性(传输大数据时使用)
                object obj = typeof(ServiceHost).Assembly.CreateInstance(
                    "System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior"
                    , true, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic
                    , null, new object[] { false, (int)point.MaxItemsInObjectGraph }, null, null);
                IServiceBehavior datacontractbehavior = obj as IServiceBehavior;
                host.Description.Behaviors.Add(datacontractbehavior);
            }
            #endregion

            host.AddServiceEndpoint(point.Contract, bing, (point.Address.StartsWith("/") ? point.Address.TrimStart('/') : point.Address));

            //自定义二进制序列化器
            if (EnableBinaryFormatterBehavior)
            {
                System.ServiceModel.Description.ServiceEndpoint spoint = host.Description.Endpoints.Count == 1 ? host.Description.Endpoints[0] : null;
                if (spoint != null && spoint.Behaviors.Find <BinaryFormatterBehavior>() == null)
                {
                    BinaryFormatterBehavior serializeBehavior = new BinaryFormatterBehavior();
                    spoint.Behaviors.Add(serializeBehavior);
                }
            }

            #region 增加拦截器处理
            if (point.Address != "Eltc.Base/FrameWork/Helper/Wcf/LoadBalance/IHeatBeat" && point.Address != "Eltc.Base/FrameWork/Helper/Wcf/Monitor/IMonitorControl")
            {
                int endpointscount          = host.Description.Endpoints.Count;
                WcfParameterInspector wcfpi = new WcfParameterInspector();
                wcfpi.WcfAfterCallEvent += new Wcf.WcfAfterCall((operationName, outputs, returnValue, correlationState, AbsolutePath) =>
                {
                    if (WcfAfterCallEvent != null)
                    {
                        WcfAfterCallEvent(operationName, outputs, returnValue, correlationState, AbsolutePath);
                    }
                });
                wcfpi.WcfBeforeCallEvent += new Wcf.WcfBeforeCall((operationName, inputs, AbsolutePath, correlationState) =>
                {
                    if (WcfBeforeCallEvent != null)
                    {
                        WcfBeforeCallEvent(operationName, inputs, AbsolutePath, correlationState);
                    }
                });
                for (int i = 0; i < endpointscount; i++)
                {
                    if (host.Description.Endpoints[i].Contract.Name != "IMetadataExchange")
                    {
                        int Operationscount = host.Description.Endpoints[i].Contract.Operations.Count;
                        for (int j = 0; j < Operationscount; j++)
                        {
                            host.Description.Endpoints[i].Contract.Operations[j].Behaviors.Add(wcfpi);
                        }
                    }
                }
            }
            #endregion

            #region 注册事件
            //错误状态处理
            host.Faulted += new EventHandler((sender, e) =>
            {
                if (WcfFaultedEvent != null)
                {
                    WcfFaultedEvent(sender, e);
                }
            });
            //关闭状态处理
            host.Closed += new EventHandler((sender, e) =>
            {
                if (WcfClosedEvent != null)
                {
                    WcfClosedEvent(sender, e);
                }

                //如果意外关闭,再次打开监听
                if (isStop)
                {
                    return;
                }

                services.Remove(host);
                OpenHost(point, debugbehavior, throtbehavior, bing, EnableBinaryFormatterBehavior);
            });
            #endregion

            host.Open();
            services.Add(host);
        }