天天看點

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;

這樣:

            binding.PortSharingEnabled = true; 

     本文轉自wengyuli 51CTO部落格,原文連結:http://blog.51cto.com/wengyuli/586701,如需轉載請自行聯系原作者