博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
客户端动态调用WCF服务中的方法
阅读量:6983 次
发布时间:2019-06-27

本文共 1014 字,大约阅读时间需要 3 分钟。

 

首先要写一个执行动态调用的方法:在里面实现反射调用。

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[] { "参数" })

转载于:https://www.cnblogs.com/anyben/p/4409035.html

你可能感兴趣的文章
高德地图POI查找
查看>>
磁盘格式化
查看>>
Fedora 11 安装指南-12
查看>>
eclipse的安卓开发插件『ADT』在线安装不成功的解决方案
查看>>
刷屏的海底捞超级APP究竟是怎样与阿里云合作的
查看>>
k8s学习笔记之三:k8s快速入门
查看>>
SpringBoot慕课学习-SpringBoot开发常用技术整合
查看>>
C10K问题
查看>>
慕课网3-13编程练习:采用flex弹性布局制作页面主导航
查看>>
线程中死锁的demo
查看>>
canvas-7globleCompositeOperation.html
查看>>
英语发音规则---H字母
查看>>
UESTC 2014 Summer Training #11 Div.2
查看>>
1035. 插入与归并(25)
查看>>
Android组件化和插件化开发
查看>>
【java】 虹软ArcFace 2.0 人脸信息识别(年龄、性别)
查看>>
Java集合--Map总结
查看>>
【转】Netty系列之Netty 服务端创建
查看>>
alpha冲刺9
查看>>
spring学习之spring 插件 for eclipse
查看>>