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

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

客户端调用wcf ,有时需要动态的调用服务端的WCF中的方法,本方法,反射wcf 的接口,动态调用接口中的方法。

主要为,动态绑定,反射动态调用。

  1. public static object ExecuteMethod<T>(string pUrl,string pMethodName, params object[] pParams)  
  2.        {  
  3.            EndpointAddress address = new EndpointAddress(pUrl);  
  4.            Binding bindinginstance = null;  
  5.            NetTcpBinding ws = new NetTcpBinding();  
  6.            ws.MaxReceivedMessageSize = 20971520;  
  7.            ws.Security.Mode = SecurityMode.None;  
  8.            bindinginstance = ws;  
  9.            using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance,address))  
  10.            {  
  11.                T instance = channel.CreateChannel();  
  12.                using (instance as IDisposable)  
  13.                {  
  14.                    try  
  15.                    {  
  16.                        Type type = typeof(T);  
  17.                        MethodInfo mi = type.GetMethod(pMethodName);  
  18.                        return mi.Invoke(instance, pParams);  
  19.                    }  
  20.                    catch (TimeoutException)  
  21.                    {  
  22.                        (instance as ICommunicationObject).Abort();  
  23.                        throw;  
  24.                    }  
  25.                    catch (CommunicationException)  
  26.                    {  
  27.                        (instance as ICommunicationObject).Abort();  
  28.                        throw;  
  29.                    }  
  30.                    catch (Exception vErr)  
  31.                    {  
  32.                        (instance as ICommunicationObject).Abort();  
  33.                        throw;  
  34.                    }  
  35.                }  
  36.            }  
  37.        }  
 

本文使用的是nettcpbinding 绑定方式,可修改。

调用方法使用

ExecuteMethod<IService>("net.tcp://192.168.0.1:8001/mex", "Test", new object[] { "参数" })

转载地址:http://jkmjl.baihongyu.com/

你可能感兴趣的文章
laravel入门-01
查看>>
类名:IExternalCommandAvailability+IExternalCommand实现对某些控件的自定义使用
查看>>
Elasticsearch的安装和使用
查看>>
Ajax简介
查看>>
java网络编程笔记
查看>>
结对作业:迷宫小游戏
查看>>
ethereumjs/ethereumjs-icap
查看>>
升级到Windows10
查看>>
转换数据库连接池为hikaricp
查看>>
第二次作业+105032014065
查看>>
KMP算法(C++版)
查看>>
九章算术卷第七 盈不足
查看>>
CCF201403-2 窗口(100分)
查看>>
关于物资管理系统
查看>>
面试题
查看>>
Html5_新标签兼容性问题
查看>>
JavaScript原型,原型链 ? 有什么特点? JavaScript如何实现继承?
查看>>
cookie注入&中转注入笔记
查看>>
java内存
查看>>
实习日记7.28
查看>>