• 客户端动态调用WCF服务中的方法


    客户端调用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[] { "参数" })

  • 相关阅读:
    SQL SERVER 查看sql语句性能与执行时间
    SQL SERVER 一组数据按规律横着放置,少则补空,如人员按一进一出的规律,进出为一组,缺少的补null
    SQL SERVER 子查询使用Order By;按In排序
    SQL SERVER 字符拆分列为多行
    SQL SERVER 字符合并多行为一列
    SQL SERVER pivot(行转列),unpivot(列转行)
    ASP.NET ViewState详解
    数字签名
    ASP.NET Form身份验证方式详解
    细说进程、应用程序域与上下文之间的关系
  • 原文地址:https://www.cnblogs.com/goody9807/p/2152594.html
Copyright © 2020-2023  润新知