C# 如何动态调用wcf,没有代理类的情况下

wangdi555 2016-07-22 10:23:02
因为是地址、方法名都不确定,无法确定接口类型。
//动态调用wcf服务
public static object CreateWCFServiceByURL<T>(string url, string bing, string pMethodName, params object[] pParams)
{
if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");
EndpointAddress address = new EndpointAddress(url);
Binding binding = CreateBinding(bing);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
T instance = factory.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 ex)
{
(instance as ICommunicationObject).Abort();
throw;
}
finally
{
GC.Collect();
}
}
}
...全文
423 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdi555 2016-07-22
  • 打赏
  • 举报
回复
不要沉啊,急需大神来看一看

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧