zoukankan      html  css  js  c++  java
  • C#动态调用WCF

        public class WcfChannelFactory
        {
            public WcfChannelFactory()
            {
            }
    
            /// <summary>
            /// 执行方法   WSHttpBinding
            /// </summary>
            /// <typeparam name="T">服务接口</typeparam>
            /// <param name="uri">wcf地址</param>
            /// <param name="methodName">方法名</param>
            /// <param name="args">参数列表</param>
            public static object ExecuteMetod<T>(string uri, string methodName, params object[] args)
            {
                //BasicHttpBinding binding = new BasicHttpBinding();   //出现异常:远程服务器返回错误: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.。
                // WSHttpBinding binding = new WSHttpBinding();
                NetTcpBinding binding = new NetTcpBinding();
                EndpointAddress endpoint = new EndpointAddress(uri);
    
                using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
                {
                    T instance = channelFactory.CreateChannel();
                    using (instance as IDisposable)
                    {
                        try
                        {
                            Type type = typeof(T);
                            MethodInfo mi = type.GetMethod(methodName);
                            return mi.Invoke(instance, args);
                        }
                        catch (TimeoutException)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (CommunicationException)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (Exception vErr)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                    }
                }
    
    
            }
    
    
            //nettcpbinding 绑定方式
            public static object ExecuteMethod<T>(string pUrl, string pMethodName, params object[] pParams)
            {
                EndpointAddress address = new EndpointAddress(pUrl);
                Binding bindinginstance = null;
                NetTcpBinding ws = new NetTcpBinding();
                ws.MaxReceivedMessageSize = 20971520;
                ws.Security.Mode = SecurityMode.None;
                bindinginstance = ws;
                using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
                {
                    T instance = channel.CreateChannel();
                    using (instance as IDisposable)
                    {
                        try
                        {
                            Type type = typeof(T);
                            MethodInfo mi = type.GetMethod(pMethodName);
                            return mi.Invoke(instance, pParams);
                        }
                        catch (TimeoutException)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (CommunicationException)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (Exception vErr)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                    }
                }
            }
        }
  • 相关阅读:
    白话排序算法--快速排序
    白话排序算法--插入排序
    javamail模拟邮箱功能--邮件回复-中级实战篇【邮件回复方法】(javamail API电子邮件实例)
    javamail模拟邮箱功能获取邮件内容-中级实战篇【内容|附件下载方法】(javamail API电子邮件实例)
    JS替换地址栏参数值
    JAVA中使用FTPClient上传下载
    js事件的捕获和冒泡阶段
    js刷新页面方法大全
    js检测上传文件大小
    java正则:不包含某个规则字符串【转】
  • 原文地址:https://www.cnblogs.com/liyangLife/p/5632384.html
Copyright © 2011-2022 走看看