コード例 #1
0
        /// <summary>
        /// 根据指定的系统编号,直接寻找WCF客户端地址
        /// </summary>
        /// <typeparam name="T">客户端代理实例</typeparam>
        /// <param name="code">系统编码</param>
        /// <param name="bindingCfgName">自己指定绑定的名称,如果绑定为空,则根据绑定类型和配置项来决定绑定配置</param>
        /// <returns>WCF接口调用类型</returns>
        public static WcfClientProxyScope <T> CreateWcfClientProxy <T>(string bindingCfgName)
        {
            try {
                Type type = typeof(T);
                RegisterTypeIfMissing(type);
                WcfClientInvokeCfgInfo wcfClientCfgInfo = _WcfInvokeContainer[type];
                if (string.IsNullOrEmpty(wcfClientCfgInfo.CredentialFileNameOrServerName))
                {
                    if (MB.Util.MyNetworkCredential.CurrentSelectedServerInfo == null)
                    {
                        throw new MB.Util.APPException("没有设置服务的配置信息,请检查节点AppSetting/WcfServers!");
                    }
                    wcfClientCfgInfo.CredentialFileNameOrServerName = MB.Util.MyNetworkCredential.CurrentSelectedServerInfo.Credential;
                }

                MB.Util.Model.WcfCredentialInfo wcfCredentialInfo = createWcfCredentialByCfgAttribute(wcfClientCfgInfo);
                string gzip = System.Configuration.ConfigurationManager.AppSettings[ENABLE_GZIP_MESSAGE];
                bool   isGZipCustomBinding = !string.IsNullOrEmpty(gzip) && string.Compare(gzip, "True", true) == 0;
                System.ServiceModel.Channels.Binding binding = createBinding(wcfCredentialInfo, isGZipCustomBinding, string.Empty);
                string uri = buildEndpointAddress(wcfCredentialInfo, wcfClientCfgInfo);
                System.ServiceModel.EndpointAddress address = new EndpointAddress(uri);
                NetworkCredential credentialInfo            = null;
                if (!string.IsNullOrEmpty(wcfCredentialInfo.UserName) && !string.IsNullOrEmpty(wcfCredentialInfo.Password))
                {
                    credentialInfo = new NetworkCredential(wcfCredentialInfo.UserName, wcfCredentialInfo.Password, wcfCredentialInfo.Domain);
                }
                T proxy = (T)createProxyInstance <T>(binding, address, credentialInfo);

                //如果每个调用都需要记录的话为产生很多日志,这里特处理一下。
                if (!_MyInvokeCache.ContainsKey(uri))
                {
                    _MyInvokeCache.Add(uri, uri);
                    MB.Util.TraceEx.Write(string.Format("开始根据地址{0} 创建WCF 客户端Channel Proxy{1}", uri, typeof(T).FullName));
                }
                WcfClientProxyScope <T> proxyScope = new WcfClientProxyScope <T>(proxy);

                return(proxyScope);
            }
            catch (MB.Util.APPException aex) {
                throw aex;
            }
            catch (Exception ex) {
                throw new MB.Util.APPException("创建WCF 客户端代理出错!", APPMessageType.SysErrInfo, ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// 创建带有Windows 安全访问Wcf 客户端代理。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="clientRule"></param>
        /// <param name="credentialInfo"></param>
        /// <returns></returns>
        public static T CreateWcfClient <T>(object clientRule, MB.Util.Model.WcfCredentialInfo credentialInfo) where T : class
        {
            try {
                if (MB.Util.MyNetworkCredential.CurrentSelectedServerInfo == null)
                {
                    throw new MB.Util.APPException("没有设置服务的配置信息!");
                }

                WcfClientInvokeAttribute cfgAtt = getWcfInvokeAtt(clientRule.GetType(), typeof(T));
                if (cfgAtt == null)
                {
                    //特殊说明: 从兼容性考虑,暂时先这里处理,以后需要去掉,要独立调用
                    return(MB.Util.MyNetworkCredential.CreateWcfClientWithCredential <T>());
                }
                string gzip = System.Configuration.ConfigurationManager.AppSettings[ENABLE_GZIP_MESSAGE];

                bool isGZipCustomBinding = !string.IsNullOrEmpty(gzip) && string.Compare(gzip, "True", true) == 0;

                MB.Util.Model.WcfCredentialInfo cfgInfo = null;
                if (credentialInfo == null)
                {
                    cfgInfo = createClientConfigInfo(MB.Util.MyNetworkCredential.CurrentSelectedServerInfo);
                }
                else
                {
                    cfgInfo = credentialInfo;
                }

                if (cfgAtt.SystemCode != SystemCode.Main)
                {
                    string appendDetail = cfgInfo.AppendDetails;
                    //从主服务的credetial转化到关联系统的credential
                    MB.Util.Model.WcfCredentialInfo cfgSubSystemInfo = resolveSubSystemCredentialInfo(cfgAtt.SystemCode, appendDetail, cfgInfo);
                    if (cfgSubSystemInfo != null)
                    {
                        cfgInfo = cfgSubSystemInfo;
                    }
                    else
                    {
                        cfgAtt.SystemCode = SystemCode.Main;
                    }
                }


                System.ServiceModel.Channels.Binding binding = createBinding(cfgInfo, isGZipCustomBinding);
                string uri = buildEndpointAddress(cfgInfo, cfgAtt);

                //如果每个调用都需要记录的话为产生很多日志,这里特处理一下。
                if (!_MyInvokeCache.ContainsKey(uri))
                {
                    _MyInvokeCache.Add(uri, uri);
                    MB.Util.TraceEx.Write(string.Format("开始根据地址{0} 创建WCF 客户端Proxy{1}", uri, typeof(T).FullName));
                }

                System.ServiceModel.EndpointAddress address = new EndpointAddress(uri);
                T proxy = (T)createProxyInstance <T>(binding, address);

                System.ServiceModel.Description.ServiceEndpoint endPoint =
                    (System.ServiceModel.Description.ServiceEndpoint)MB.Util.MyReflection.Instance.InvokePropertyForGet(proxy, "Endpoint");


                object obj = typeof(System.ServiceModel.ServiceHost).Assembly.CreateInstance("System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior",
                                                                                             true, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { false, Int32.MaxValue }, null, null);

                IEndpointBehavior dataSerializerBehavior = obj as IEndpointBehavior;
                endPoint.Behaviors.Add(dataSerializerBehavior);

                //增加分页消息头
                endPoint.Behaviors.Add(new WcfDataQueryBehavior());

                //增加性能指标检测行为
                string wcfMonitorEnable   = System.Configuration.ConfigurationManager.AppSettings[ENABLE_WCF_PERFORMANCE_MONITOR];
                bool   isWcfMonitorEnable = string.IsNullOrEmpty(wcfMonitorEnable) || string.Compare(gzip, "True", true) == 0;
                if (isWcfMonitorEnable)
                {
                    endPoint.Behaviors.Add(new MB.Util.Monitors.WcfPerformanceMonitorBehavior());
                }

                #region 加载应用程序自定义的behavior

                //增加扩展的Behavior,从配置文件中读取配置的扩展Behavior
                //在APPSetting中每个扩展由分号隔开,类型和Assmbly之间由逗号隔开
                string customerBehaviorSrc = System.Configuration.ConfigurationManager.AppSettings[CUSTOM_ENDPOINT_BEHAVIORS];
                if (!string.IsNullOrEmpty(customerBehaviorSrc))
                {
                    string[] customerBehaviors = customerBehaviorSrc.Split(';');
                    foreach (string customerBehavior in customerBehaviors)
                    {
                        try {
                            string[] beTypes     = customerBehavior.Split(',');
                            object   extBehavior = DllFactory.Instance.LoadObject(beTypes[0], beTypes[1]);
                            if (extBehavior != typeof(WcfDataQueryBehavior) &&
                                extBehavior != typeof(MB.Util.Monitors.WcfPerformanceMonitorBehavior))
                            {
                                IEndpointBehavior extEndPointBehavior = extBehavior as IEndpointBehavior;
                                endPoint.Behaviors.Add(extEndPointBehavior);
                            }
                        }
                        catch (Exception ex) {
                            MB.Util.TraceEx.Write(string.Format("加载自定义behvaior出错:{0};错误:{1}", customerBehavior, ex.ToString()));
                        }
                    }
                }
                #endregion

                if (!isGZipCustomBinding)
                {
                    if (cfgInfo.StartWindowsCredential && !string.IsNullOrEmpty(cfgInfo.UserName))
                    {
                        System.Net.NetworkCredential credential = MB.Util.MyNetworkCredential.CreateNetworkCredential(cfgInfo);
                        System.ServiceModel.Description.ClientCredentials clientCredentials =
                            (System.ServiceModel.Description.ClientCredentials)MB.Util.MyReflection.Instance.InvokePropertyForGet(proxy, "ClientCredentials");

                        clientCredentials.Windows.ClientCredential = credential;
                    }
                }

                return(proxy);
            }
            catch (MB.Util.APPException aex) {
                throw aex;
            }
            catch (Exception ex) {
                throw new MB.Util.APPException("创建WCF 客户端代理出错!", APPMessageType.SysErrInfo, ex);
            }
        }