动态调用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)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
}
在客户端调用:
ExecuteMethod<IService>("net.tcp://192.168.0.1:8001/mex", "Test", new object[] { "参数" })
这句话什么意思,为什么老是说当前上下文中不存在名称“ExecuteMethod”,未能找到类型或命名空间名称“IService”(是否缺少 using 指令或程序集引用?)
{
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)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
}
在客户端调用:
ExecuteMethod<IService>("net.tcp://192.168.0.1:8001/mex", "Test", new object[] { "参数" })
这句话什么意思,为什么老是说当前上下文中不存在名称“ExecuteMethod”,未能找到类型或命名空间名称“IService”(是否缺少 using 指令或程序集引用?)
...全文
相关推荐
WCF.rar_ChannelFactory_WCF DynamicProxy_WCF 动态_wcf 动态调用_动态WCF
一个简单的动态调用wcf类,在公司的服务器上使用稳定。
WCF入门及WinForm中动态调用
WCF入门及WinForm中动态调用,内容说明地址:http://www.cnblogs.com/George-Qiao/p/4370930.html
总结C#动态调用WCF接口的两种方法
给大家总结了C#动态调用WCF接口的两种方法,大家可以根据自己的需求选择对应的方式,下面来一起看看。
WcfHelper-WCF调用示例
WcfHelper-WCF调用示例
WCF客户端动态设置WCF服务器主机.doc
服务器


