• 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;
                        }
                    }
                }
            }
        }
  • 相关阅读:
    sqli-libs(3)
    python学习之路(18)
    BZOJ3534:[SDOI2014]重建——题解
    洛谷省选斗兽场全通关祭~以及之后的打算!
    BZOJ4596:[SHOI2016]黑暗前的幻想乡——题解
    BZOJ2732:[HNOI2012]射箭——题解
    BZOJ1486:[HNOI2009]最小圈——题解
    BZOJ4552:[HEOI2016/TJOI2016]排序——题解
    BZOJ2830 & 洛谷3830:[SHOI2012]随机树——题解
    BZOJ4889 & 洛谷3759:[TJOI2017]不勤劳的图书管理员——题解
  • 原文地址:https://www.cnblogs.com/liyangLife/p/5632384.html
Copyright © 2020-2023  润新知