动态调用WCF服务[转]
2010-07-29 09:51
507 查看
原文地址:http://blog.csdn.net/castlooo/archive/2010/05/06/5562619.aspx
客户端调用wcf ,有时需要动态的调用服务端的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;
}
}
}
}
本文使用的是nettcpbinding 绑定方式,可修改。
调用方法使用
ExecuteMethod<IService>("net.tcp://192.168.0.1:8001/mex", "Test", new object[] { "参数" })
另外还有一篇贴子可参考:http://hi.baidu.com/meback/blog/item/c140495447258e5d564e0006.html
客户端调用wcf ,有时需要动态的调用服务端的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;
}
}
}
}
本文使用的是nettcpbinding 绑定方式,可修改。
调用方法使用
ExecuteMethod<IService>("net.tcp://192.168.0.1:8001/mex", "Test", new object[] { "参数" })
另外还有一篇贴子可参考:http://hi.baidu.com/meback/blog/item/c140495447258e5d564e0006.html
相关文章推荐
- 客户端动态调用WCF服务中的方法
- 记录:Web无引用无配置方式动态调用WCF服务
- 创建一个简单的WCF程序2——手动开启/关闭WCF服务与动态调用WCF地址
- Silverlight里动态调用WCF服务
- 客户端动态调用WCF服务中的方法
- [转载]客户端动态调用WCF服务中的方法
- 动态调用WCF服务
- 客户端动态调用WCF服务中的方法
- 动态调用WCF服务
- 动态调用WCF不添加服务(svcutil.exe)
- 动态调用WCF服务
- 动态调用wcf接口服务
- 动态调用webservice及WCF服务
- 动态调用WCF服务
- silverlight动态调用WCF服务解决办法
- WCF服务通过 SvcUtil.exe 方式调用,WCF半动态调用
- [WCF-Discovery] 实例演示:如何利用服务发现机制实现服务的“动态”调用?
- WCF客户端调用服务时配置产生的错误
- WCF分布式开发常见错误(3):客户端调用服务出错:You have tried to create a channel to a service that does not support .Net Framing
- 在iis下发布wcf,调用时出现错误--调用方未由服务进行身份验证