WCF客户端动态设置WCF服务器主机的地址的方法参考,可以连接多个相同WCF主机的方法

简介:

 最近做一个项目,需要在客户端灵活配置连接到哪个服务器的功能,例如客户端是一个,现在想连接A服务器就连A服务器,想连接B服务器就连接B服务器,当然不需要手动修改配置文件,直接通过程序来实现WCF目标主机的配置功能。

  参考核心代码如下:

// --------------------------------------------------------------------
//  All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. 
// --------------------------------------------------------------------

using  System.ServiceModel;

namespace  DotNet.WCFClient
{
using  DotNet.IService;
using  DotNet.Utilities;

/// <summary>
///  ServiceFactory
///  本地服务的具体实现接口
///
///  修改纪录
///
///         2011.07.03 版本:2.0 JiRiGaLa 可以动态指定服务器地址的调用方法。
///         2009.09.20 版本:1.0 JiRiGaLa 创建。
///
///  版本:2.0
///
/// <author>
/// <name> JiRiGaLa </name>
/// <date> 2011.07.03 </date>
/// </author>
/// </summary>
public class  ServiceFactory : IServiceFactory
   {
private string  host  = string .Empty;
/// <summary>
///  主机地址
///  Host = "192.168.0.122";
/// </summary>
public string  Host
       {
get
           {
return  host;
           }
set
           {
               host 
=  value;
           }
       }

private int  port  = 0 ;
/// <summary>
///  端口号
/// </summary>
public int  Port
       {
get
           {
return  port;
           }
set
           {
               port 
=  value;
           }
       }

public  ServiceFactory()
       {
       }

/// <summary>
///  初始化服务
/// </summary>
public void  InitService()
       {
       }

/// <summary>
///  动态设定WCF主机地址端口的方法
/// </summary>
/// <param name="address"> 主机地址 </param>
/// <returns> 主机地址 </returns>
public virtual  EndpointAddress GetHotsUrl(EndpointAddress address)
       {
//  若当前配置都是空的,就不用生成新的URL了。
if  ( string .IsNullOrEmpty(Host)  &&  (Port  == 0 ))
           {
return  address;
           }
//  判断当前配置的情况
string  endpointAddress  = string .Empty;
if  ( string .IsNullOrEmpty(Host))
           {
               Host 
=  address.Uri.Host;
           }
if  (Port  == 0 )
           {
               Port 
=  address.Uri.Port;
           }
           endpointAddress 
=  address.Uri.Scheme  + " :// " +  Host  + " : " +  Port.ToString()  +  address.Uri.LocalPath;
           address 
= new  EndpointAddress(endpointAddress);
return  address;
       }

public virtual  ISequenceService CreateSequenceService()
       {
           ChannelFactory
< ISequenceService >  channelFactory  = new  ChannelFactory < ISequenceService > ( " DotNet.Service.SequenceService " );
           channelFactory.Endpoint.Address 
=  GetHotsUrl(channelFactory.Endpoint.Address);
//  加强安全验证防止未授权匿名调用
           channelFactory.Credentials.UserName.UserName  =  BaseSystemInfo.ServiceUserName;
           channelFactory.Credentials.UserName.Password 
=  BaseSystemInfo.ServicePassword;
           ISequenceService proxy 
=  channelFactory.CreateChannel();
return  proxy;
       }

      }
   }
}

  配套的配置文件参考如下:

<? xml version = " 1.0 " ?>
< configuration >
< system.serviceModel >
< client >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/ExceptionService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IExceptionService "  name = " DotNet.Service.ExceptionService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/FileService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IFileService "  name = " DotNet.Service.FileService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/FolderService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IFolderService "  name = " DotNet.Service.FolderService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/ItemDetailsService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IItemDetailsService "  name = " DotNet.Service.ItemDetailsService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/ItemsService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IItemsService "  name = " DotNet.Service.ItemsService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/LogOnService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.ILogOnService "  name = " DotNet.Service.LogOnService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/LogService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.ILogService "  name = " DotNet.Service.LogService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/MessageService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IMessageService "  name = " DotNet.Service.MessageService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/ModuleService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IModuleService "  name = " DotNet.Service.ModuleService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/OrganizeService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IOrganizeService "  name = " DotNet.Service.OrganizeService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/ParameterService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IParameterService "  name = " DotNet.Service.ParameterService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/PermissionItemService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IPermissionItemService "  name = " DotNet.Service.PermissionItemService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/PermissionService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IPermissionService "  name = " DotNet.Service.PermissionService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/RoleService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IRoleService "  name = " DotNet.Service.RoleService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/SequenceService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.ISequenceService "  name = " DotNet.Service.SequenceService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/StaffService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IStaffService "  name = " DotNet.Service.StaffService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/UserService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IUserService "  name = " DotNet.Service.UserService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/BusinessDbHelperService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IDbHelperService "  name = " DotNet.Service.BusinessDbHelperService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/UserCenterDbHelperService/ "  binding = " netTcpBinding " bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IDbHelperService "  name = " DotNet.Service.UserCenterDbHelperService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/WorkFlowActivityAdminService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IWorkFlowActivityAdminService "  name = " DotNet.Service.WorkFlowActivityAdminService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/WorkFlowCurrentService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IWorkFlowCurrentService "  name = " DotNet.Service.WorkFlowCurrentService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/WorkFlowProcessAdminService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.IWorkFlowProcessAdminService "  name = " DotNet.Service.WorkFlowProcessAdminService " ></ endpoint >
< endpoint address = " net.tcp://jirigala-PC:8888/DotNet.Service/TableColumnsService/ "  binding = " netTcpBinding "  bindingConfiguration = " netTcpBindingSmiple "  contract = " DotNet.IService.ITableColumnsService "  name = " DotNet.Service.TableColumnsService " ></ endpoint >
</ client >

< bindings >
< netTcpBinding >
< binding name = " netTcpBindingSmiple "  maxBufferPoolSize = " 2147483647 "  maxReceivedMessageSize = " 2147483647 "  closeTimeout = " 00:00:05 "  openTimeout = " 00:00:02 "  receiveTimeout = " 10:10:10 "  sendTimeout = " 00:00:30 " >
< security mode = " Message " >
< message clientCredentialType = " UserName " />
</ security  >
</ binding >
</ netTcpBinding >
</ bindings >

</ system.serviceModel >
</ configuration >




本文转自 jirigala 51CTO博客,原文链接:http://blog.51cto.com/2347979/1197391,如需转载请自行联系原作者
相关文章
|
1月前
|
存储 人工智能 缓存
阿里云服务器五代至九代实例规格详解及性能提升对比,场景适配与选择指南参考
目前阿里云服务器的实例规格经过多次升级之后,最新一代已经升级到第九代实例,当下主售的云服务器实例规格也以八代和九代云服务器为主,对于初次接触阿里云服务器实例规格的用户来说,可能并不是很清楚阿里云服务器五代、六代、七代、八代、九代实例有哪些,他们之间有何区别,下面小编为大家介绍下阿里云五代到九代云服务器实例规格分别有哪些以及每一代云服务器在性能方面具体有哪些提升,以供大家参考和了解。
226 15
|
1月前
|
弹性计算 运维 安全
阿里云轻量应用服务器38元1年和云服务器99元1年怎么选?二者性能区别及选择参考
在阿里云当下的活动中,38元/年的轻量应用服务器与99元/年的云服务器ECS成为众多新用户的关注焦点。但是有部分用户并不是很清楚二者之间的区别,因此就不知道应该如何选择。接下来,笔者将为您详细剖析ECS云服务器与轻量应用服务器的差异,以供您参考和选择。
305 4
阿里云轻量应用服务器38元1年和云服务器99元1年怎么选?二者性能区别及选择参考
|
1月前
|
存储 弹性计算 Linux
阿里云账号注册、领取优惠券、试用云服务器和购买云服务器教程参考
对于还未使用阿里云任何云产品的用户来说,完整的上云流程包括账号注册并完成实名认证,然后才是试用或者购买云服务器,有的新手用户对这一流程还不是很清楚。本文为大家介绍新手用户从注册阿里云账号,完成实名认证,然后领取阿里云优惠券,并试用云服务器和购买云服务器的全部流程,适合初次购买和试用阿里云服务器的新手用户参考。