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,如需转载请自行联系原作者
相关文章
|
2月前
|
Java
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
java小工具util系列5:java文件相关操作工具,包括读取服务器路径下文件,删除文件及子文件,删除文件夹等方法
79 9
|
2月前
|
缓存 监控 定位技术
|
8天前
|
SQL 存储 关系型数据库
MySQL/SqlServer跨服务器增删改查(CRUD)的一种方法
通过上述方法,MySQL和SQL Server均能够实现跨服务器的增删改查操作。MySQL通过联邦存储引擎提供了直接的跨服务器表访问,而SQL Server通过链接服务器和分布式查询实现了灵活的跨服务器数据操作。这些技术为分布式数据库管理提供了强大的支持,能够满足复杂的数据操作需求。
52 12
|
2月前
|
弹性计算 异构计算
2024年阿里云GPU服务器多少钱1小时?亲测价格查询方法
2024年阿里云GPU服务器每小时收费因实例规格不同而异。可通过阿里云GPU服务器页面选择“按量付费”查看具体价格。例如,NVIDIA A100的gn7e实例为34.742元/小时,NVIDIA A10的gn7i实例为12.710156元/小时。更多详情请访问阿里云官网。
201 2
|
2月前
|
人工智能 弹性计算 关系型数据库
学生免费领取阿里云服务器一年的方法,以及各种活动
学生可以免费领取阿里云服务器一年,新人可获2核4G,非新人2核2G。访问链接注册并完成学生认证,领取300元无门槛优惠券,购买轻量应用服务器。此外,还有多项活动可赢取实物奖品。
545 2
|
2月前
|
监控 Kubernetes 安全
如何设置一个有效的远程管理工具来简化服务器的维护工作?
如何设置一个有效的远程管理工具来简化服务器的维护工作?
|
3月前
|
Web App开发 安全 网络安全
tplink虚拟服务器设置方法
为了更全面地理解云服务及其在企业应用中的角色,推荐访问,他们提供了一系列高性能、安全稳定的云服务器解决方案,包括但不限于香港云服务器、高防服务器等,特别适合寻求全球化业务扩展的企业。蓝易云不仅拥有全球化的基础设施布局,还提供针对各种行业定制的全栈云解决方案,助力企业实现云端部署,跨越传统界限,即刻启航云端之旅。
73 0
|
3月前
|
前端开发 Docker 容器
主机host服务器和Docker容器之间的文件互传方法汇总
Docker 成为前端工具,可实现跨设备兼容。本文介绍主机与 Docker 容器/镜像间文件传输的三种方法:1. 构建镜像时使用 `COPY` 或 `ADD` 指令;2. 启动容器时使用 `-v` 挂载卷;3. 运行时使用 `docker cp` 命令。每种方法适用于不同场景,如静态文件打包、开发时文件同步及临时文件传输。注意权限问题、容器停止后的文件传输及性能影响。
652 0
|
3月前
|
Apache 数据中心 Windows
将网站迁移到阿里云Windows系统云服务器,访问该站点提示连接被拒绝,如何处理?
将网站迁移到阿里云Windows系统云服务器,访问该站点提示连接被拒绝,如何处理?
|
2天前
|
机器学习/深度学习 人工智能 PyTorch
阿里云GPU云服务器怎么样?产品优势、应用场景介绍与最新活动价格参考
阿里云GPU云服务器怎么样?阿里云GPU结合了GPU计算力与CPU计算力,主要应用于于深度学习、科学计算、图形可视化、视频处理多种应用场景,本文为您详细介绍阿里云GPU云服务器产品优势、应用场景以及最新活动价格。
阿里云GPU云服务器怎么样?产品优势、应用场景介绍与最新活动价格参考