2016-11-07 50 views
0

我有兩個應用程序,一個使用WCF服務,另一個使用WCF客戶端。WCF客戶端無法連接到具有動態端口的服務

當我使用靜態端口時,它們之間的連接工作正常。

當我傳遞一個「0」作爲端口號時,WCF服務動態獲得一個可用的端口。

雖然客戶端獲取端口並將該端口傳遞給服務器,但連接始終以「EndpointNotFoundException」和「地址過濾器不匹配」結尾。

我評論說「元數據」綁定出來,因爲它沒有幫助。

//IP address is determined by code, for simplicity in this example it is hardcoded 
//set port to 0 to get a free port 
var url = $"net.tcp://190.150.140.22:0/UiHost"; 
var UiHost = new ServiceHost(typeof(ShippingUIService), new Uri(url)); 

//var mBehave = new ServiceMetadataBehavior(); 
//UiHost.Description.Behaviors.Add(mBehave); 

var ntb = new NetTcpBinding(SecurityMode.None) { ListenBacklog = 10, MaxConnections = 20, PortSharingEnabled = false }; 

var endPoint = UiHost.AddServiceEndpoint(typeof(Core.IShippingUIService), ntb, ""); 
//UiHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); 
// Tell WCF to actually bind to a free port instead of 0 
endPoint.ListenUriMode = System.ServiceModel.Description.ListenUriMode.Unique; 

UiHost.Open(); 

//Uri is saved, so the client can access the service 
var serviceHostUri = UiHost.ChannelDispatchers.First().Listener.Uri.AbsoluteUri; 
log.Info($"UI Service started. With address {serviceHostUri}"); 

是否有可能這一點的代碼,沒有返回給定的實際端口號?

UiHost.ChannelDispatchers.First()。Listener.Uri.AbsoluteUri;

感謝您提前的每一個提示。

回答

0

您的服務主機的默認地址過濾器是在不知道動態分配端口的情況下生成的,因此不匹配。最簡單的解決方案可能是將您的服務類型(ShippingUIService)設置爲使用AddressFilterMode = AddressFilterMode.Any在任何地址上進行響應。

的代碼基本位:

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] 
class ShippingUIService { 
    // Class members 
} 
+0

非常感謝,這有助於! – Hiram

相關問題