读书人

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

发布时间: 2013-11-25 13:22:27 作者: rapoo

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动态调用的实例,

何为方法契约,何为服务,何为数据契约

读书人网 >Web Service

热点推荐