惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
O
OpenAI News
美团技术团队
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
S
Security Affairs
博客园_首页
S
Schneier on Security
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
Vercel News
Vercel News
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 最新话题
D
DataBreaches.Net
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Check Point Blog
H
Hacker News: Front Page

博客园 - Danny Chen

python asyncio 获取协程返回值和使用callback c#等待所有子线程执行完毕方法 Python virtualenv 查看当前正在运行的python进程 Mac 删除/卸载 自己安装的python python读取txt文件最后一行(文件大+文件小) pycharm 注册码/License server 2017年最新 Linux cat命令详解 在Mac平台上安装配置ELK时的一些总结 Mac上搭建ELK C#中用schema验证xml的合法性 C#中XML与对象之间的序列化、反序列化 C#位运算 SQL Server 权限管理 如何用C#动态编译、执行代码 [C#]手把手教你打造Socket的TCP通讯连接(一) C#动态调用WCF接口,两种方式任你选。 矩阵的坐标变换(转) 【.NET线程--进阶(一)】--线程方法详解
动态调用WCF服务
Danny Chen · 2017-05-02 · via 博客园 - Danny Chen

本文转载:http://www.cnblogs.com/wiseant/archive/2010/07/29/1787599.html

原文地址: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