zoukankan      html  css  js  c++  java
  • 动态的调用服务端的WCF中的方法

    客户端调用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

  • 相关阅读:
    python多线程爬虫:亚马逊价格
    python在linux中输出带颜色的文字的方法
    单线程爬虫VS多线程爬虫的效率对比
    python爬虫:正则表达式
    爬虫-python调用百度API/requests
    Python gevent学习笔记-2
    Python gevent学习笔记
    IO多路复用之select总结
    select、poll、epoll之间的区别总结[整理]
    2020年 IEDA破解码失效,2019 版IDEA无法使用 ,已解决,有效期2100年;原标题:IDEA激活—免费永久激活(lookdiv.com)
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/6710018.html
Copyright © 2011-2022 走看看