WCF客户端动态设置WCF服务器主机的地址的方法参考,可以连接多个相同WCF主机的方法-阿里云开发者社区

开发者社区> 科技小能手> 正文

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
oracle取客户端网卡地址
http://www.itpub.net/24114.html select distinct utl_inaddr.get_host_address(terminal),terminal from v$session;
475 0
搭建云服务器主机具体方法是什么?
云服务器的出现给我们带来了极大的方便,但是很多人对于云服务器的了解还不多,不知道怎么搭建主机。下面笔者就给大家介绍搭建云服务器主机,搭建的具体方法是什么?
2477 0
I.MX6 android 设置 默认 动态桌面
/************************************************************************ * I.MX6 android 设置 默认 动态桌面 * 声明: * 对于Android的桌面,调试显示部分的时候还是喜欢用动态的桌面进行调试, * 感觉就是会好点一样。
588 0
Mac修改主机host地址
Mac修改主机host地址
15 0
请问云虚拟主机和云服务器ECS的区别是什么
请问云虚拟主机和云服务器ECS的区别是什么 我想做一个小型网站,不知道适合这两款的哪个好。 云服务器也叫云主机,它是在一组集群服务器上划分出来的多个类似独立主机的部分.它与服务器一样,有独立的IP.
5485 0
23706
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载