简体   繁体   English

将WCF NetTcpBinding设置为仅接受本地连接时出错

[英]Error when setting WCF NetTcpBinding to only accept local connections

I am trying to set up a WCF service which only accepts incoming messages/connection from itself. 我正在尝试设置一个WCF服务,它只接受来自自身的传入消息/连接。

I have been able to successfully create the service and run it and communicate with it using this code to create the WCF Endpoint (not restricted to localhost only) 我已经能够成功创建服务并运行它并使用此代码与它通信以创建WCF端点(仅限于localhost)

NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
_host = new ServiceHost(this, new Uri("net.tcp://localhost:19852"));
_host.Description.Behaviors.Add(new ServiceMetadataBehavior());
_host.AddServiceEndpoint(typeof(ISyncClient), binding, "SyncService");
_host.AddServiceEndpoint(typeof(IMetadataExchange), System.ServiceModel.Description.MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
_host.Open();

As soon as I add this line to restrict to connections from localhost 只要我添加此行以限制来自localhost的连接

binding.HostNameComparisonMode = HostNameComparisonMode.Exact;

I get this exception 我得到了这个例外

System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:19852. System.ServiceModel.AddressAlreadyInUseException:IP端点0.0.0.0:19852上已有一个侦听器。 This could happen if there is another application already listening on this endpoint or if you have multiple service endpoints in your service host with the same IP endpoint but with incompatible binding configurations. 如果有另一个应用程序已在此端点上侦听,或者如果服务主机中有多个服务端点具有相同的IP端点但具有不兼容的绑定配置,则可能会发生这种情况。 ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted ---> System.Net.Sockets.SocketException:通常只允许使用每个套接字地址(协议/网络地址/端口)

I'm not even sure what I am doing is the correct way to restrict WCF access, but obviously its not working. 我甚至不确定我在做什么是限制WCF访问的正确方法,但显然它不起作用。 To me this looks like some sort of conflict with the MEX endpoint. 对我来说,这看起来与MEX端点有某种冲突。 As far as I know I NEED the mex endpoint so I can't get rid of it. 据我所知,我需要mex端点,所以我无法摆脱它。 Anyone point me in the direction of a solution? 有人指出我的解决方案吗?

The easy way to do this is with a named pipe binding. 执行此操作的简单方法是使用命名管道绑定。 It only supports local calls. 它只支持本地电话。 From Choosing a Transport : 选择运输

When communication is required between different WCF applications on a single computer, and you want to prevent any communication from another machine, then use the named pipes transport. 当在一台计算机上的不同WCF应用程序之间需要通信,并且您希望阻止来自另一台计算机的任何通信时,请使用命名管道传输。

Also, Mex points are completely optional. 此外,Mex积分是完全可选的。 You can get rid of its endpoint and behavior without a problem. 您可以毫无问题地摆脱其端点和行为。

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

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