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_bao 51CTO博客,原文链接:http://blog.51cto.com/jirigala/812467

上一篇:阿里云托管网格服务入门与应用部署实践(上)


下一篇:任务管理-轻松搞定 IoT 设备重启、资源包更新、固件升级等业务