WCF常见问题之端口共享

本文详细介绍了在使用WCF时遇到的端口冲突问题,通过开启WCF的端口共享功能并启动一个特定服务来解决该问题。包括代码示例和配置步骤。

当两个WCF应用程序想共享一个端口提供服务时,你会发现后一个启动的服务运行不了,会提示已经有应用程序在监听,如:

应用一

 

代码
ServiceHost host = new ServiceHost(typeof(WCFPortSharing.AddService)); 
            NetTcpBinding binding 
= new NetTcpBinding(); 
            host.AddServiceEndpoint(
typeof(WCFPortSharing.IAddService), binding, "net.tcp://127.0.0.1:4503/addService"); 
            host.Open();

 

应用二

 

代码
ServiceHost host1 = new ServiceHost(typeof(WCFPortSharing.SubService)); 
            NetTcpBinding binding1 
= new NetTcpBinding(); 
            host1.AddServiceEndpoint(
typeof(WCFPortSharing.ISubService), binding1, "net.tcp://127.0.0.1:4503/subService"); 
            host1.Open();

 

如果想不冲突,需要开启WCF的端口共享,并启动一个服务Net.Tcp Port Sharing Service,此服务默认是禁止运行的,手动开启后,将服务配置加上

 

binding.PortSharingEnabled = true;

 

这样:

代码
ServiceHost host = new ServiceHost(typeof(WCFPortSharing.AddService)); 
            NetTcpBinding binding 
= new NetTcpBinding(); 
            binding.PortSharingEnabled 
= true
            host.AddServiceEndpoint(
typeof(WCFPortSharing.IAddService), binding, "net.tcp://127.0.0.1:4503/addService"); 
            host.Open();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值