简体   繁体   English

WCF客户端无法通过动态端口连接到服务

[英]WCF client cannot connect to service with dynamic port

I have 2 applications one with a WCF Service and another one with a WCF client. 我有2个应用程序,一个带有WCF服务,另一个带有WCF客户端。

The connection between them works fine when I use a static port. 当我使用静态端口时,它们之间的连接工作正常。

When I pass a "0" as the port number the WCF service gets dynamically an available port. 当我传递“ 0”作为端口号时,WCF服务会动态获得可用端口。

Although the client gets the port and passes that port to the server, a connection always ends with an "EndpointNotFoundException" and and "Address Filter Mismatch". 尽管客户端获取端口并将该端口传递给服务器,但连接始终以“ EndpointNotFoundException”和“地址过滤器不匹配”结束。

I commented the "MetaData" binding out as it did not help. 我注释了“ MetaData”绑定,因为它没有帮助。

//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}");

Is it possible that this bit of code, does not return the actual port number given? 这段代码是否可能不返回给定的实际端口号?

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

Thanks for every hint in advance. 感谢您提前提供的所有提示。

The default address filter for Your service host is generated without knowledge of the dynamically assigned port, and does therefore not match. 您的服务主机的默认地址过滤器是在不知道动态分配的端口的情况下生成的,因此不匹配。 The easiest solution is probably to set Your service type (ShippingUIService) to respond on any address, using AddressFilterMode = AddressFilterMode.Any. 最简单的解决方案可能是使用AddressFilterMode = AddressFilterMode.Any将您的服务类型(ShippingUIService)设置为在任何地址上响应。

The essential bit of code: 基本代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM