游戏技术文章

动态调用WCF

时间:2016-12-9 10:44:57  作者:棋牌资源网  来源:棋牌资源网  查看:7725  评论:0
内容摘要:最近在做的一个项目中需要动态调用WCF地址,因为有很多终端服务器,而每台终端服务器上都部署一个WCF服务,中央服务器需要不定时调用其中某个或者多个WCF服务执行相关操作,因此添加引用及配置文件配置的方法就不太现实,以下提供两种动态调用WCF地址的方法:1、使用ChannelFac...
最近在做的一个项目中需要动态调用WCF地址,因为有很多终端服务器,而每台终端服务器上都部署一个WCF服务,中央服务器需要不定时调用其中某个或者多个WCF服务执行相关操作,因此添加引用及配置文件配置的方法就不太现实,以下提供两种动态调用WCF地址的方法:

1、使用ChannelFactory类,该方法虽然复杂了点,但灵活度最高:

code:


/* ========================================================================
 * 【本类功能概述】   ChannelFactory 类创建多个终结点侦听器
 * 
 * 作者:EricHu       时间:2012/6/5 14:14:54
 * 文件名:WcfChannelFactory
 * CLR版本:4.0.30319.235
 *
 * 修改者:           时间:              
 * 修改说明:
 * ========================================================================
 */
 
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.ServiceModel;
 using System.ServiceModel.Channels;
 using System.Reflection;
 
 namespace JJInn.Baselibray.Common
 {
     /// <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, params object[] 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,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;
                         }
                     }
                 }
         }
     }
 }
 

 

调用示例:

1
2
3
4
string uri = "http://localhost:9998/mywcf/Service";
object o = ExecuteMetod<IService>(uri, "Add",12.0,13.0);
Console.WriteLine(o.ToString());
Console.ReadKey();

 

2、简单方法,不考虑太多,直接调用:

1
2
3
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());

标签:动态调用WCF 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved