• C#动态调用WCF接口(3)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Reflection;
    
    namespace wcf.wcfbase
     {
        ///<summary>
        /// 使用ChannelFactory为wcf客户端创建独立通道
        ///</summary>
        public class WcfChannelFactory
        {
            public WcfChannelFactory()
            {
            }
    
            ///<summary>
            /// 执行方法 WSHttpBinding
            ///</summary>
            ///<typeparam name="T">服务接口</typeparam>
            ///<param name="uri">wcf地址</param>
            ///<param name="methodName">方法名</param>
            ///<param name="args">参数列表</param>
            public static object ExecuteMetod<T>(string uri, string methodName, paramsobject[] args)
            {
                //BasicHttpBinding binding = new BasicHttpBinding(); //出现异常:远程服务器返回错误: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.。
                 WSHttpBinding binding = new WSHttpBinding();
                 EndpointAddress endpoint = new EndpointAddress(uri);
    
                using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
                {
                    T instance = channelFactory.CreateChannel();
                    using (instance as IDisposable)
                    {
                        try
                        {
                            Type type = typeof(T);
                            MethodInfo mi = type.GetMethod(methodName);
                            return mi.Invoke(instance, args);
                        }
                        catch (TimeoutException)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (CommunicationException)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (Exception vErr)
                        {
                            (instance as ICommunicationObject).Abort();
                            throw;
                        }
                    }
                 }
             }
    
    
            //nettcpbinding 绑定方式
            public static object ExecuteMethod<T>(string pUrl, string pMethodName,paramsobject[] 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;
                        }
                    }
                }
            }
            
            //调用示例:
            string uri = "http://localhost:9998/mywcf/Service";
            object o = ExecuteMetod<IService>(uri, "Add",12.0,13.0);
            Console.WriteLine(o.ToString());
            Console.ReadKey();
            
            //简单方法,不考虑太多,直接调用:
            EndpointAddress address1 = new EndpointAddress("http://localhost:9998/mywcf/Service");
            ServiceClient service1 = new ServiceClient(new WSHttpBinding(), address1);
            Console.WriteLine(service1.Add(12.0, 13.0).ToString());
        }
     }
  • 相关阅读:
    不用SMTP服务测试邮件代码[译]
    我的2007鲜有收获,充满彷徨
    AspNet上传文件的几个控件(downmoon收集)
    去掉Firefox的自动缩放记忆(downmoon)
    outlook Menu的一些资源(downmoon收集)
    log4net写入到SQL server的基本配置(downmoon)
    .net读取Windows登录用户信息(downmoon)
    微软提供对汉语拼音的强大升级支持Microsoft Visual Studio International Pack 1.0 SR1
    Access/MSSQL/Oracle/MySql获取当前用户连接数(downmoon收集)
    UML建模的要点总结(一)
  • 原文地址:https://www.cnblogs.com/rinack/p/5675895.html
Copyright © 2020-2023  润新知