当前位置: 代码迷 >> Web Service >> WCF有关问题,动态调用wcf,正解立即结贴
  详细解决方案

WCF有关问题,动态调用wcf,正解立即结贴

热度:628   发布时间:2013-11-25 13:22:27.0
WCF问题,动态调用wcf,正解立即结贴
        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;
                    }
                }
            }
        }
    }

怎么调这个方法,ExecuteMethod<IService>("net.tcp://192.168.0.1:8001/mex", "Test", new object[] { "参数" });不明白这个IService要怎么生成,怎么来使用。为TChannel,但怎么来生成这个IService.

------解决方案--------------------
你可以看看WCF动态调用的实例,

何为方法契约,何为服务,何为数据契约
  相关解决方案