请原谅我,因为我是WCF服务/Windows服务的新手。我创建了一个托管在Windows服务中的WCF服务。我想通过TCP在Silverlight浏览器内应用程序中使用WCF服务。下面是Silverlight中访问WCF服务的代码片段:
var messageEncoding = new BinaryMessageEncodingBindingElement();
var tcpTransport = new TcpTransportBindingElement();
var binding = new CustomBinding(messageEncoding, tcpTransport);
// Create a channel factory for the service endpoint configured with the custom binding.
var cf = new ChannelFactory<ICalcService>(binding, new EndpointAddress("net.tcp://192.168.2.104:4508"));
// Open the channel.
ICalcService channel = cf.CreateChannel();
// Invoke the method asynchronously.
channel.BeginAdd(9, 5, AddCallback, channel);
private void AddCallback(IAsyncResult asyncResult)
{
try
{
double endAdd = ((ICalcService) asyncResult.AsyncState).EndAdd(asyncResult);
}
catch (Exception exception)
{
throw exception;
}
}
代码有时工作得很好,但经常会抛出一个臭名昭著的System.ServiceModel。由于某些原因,CommunicationException返回以下消息:
Could not connect to net.tcp://192.168.2.104:4508/. The connection attempt lasted for a time span of 00:00:00.1010058. TCP error code 10013: An attempt was made to access a socket in a way forbidden by its access permissions.. This could be due to attempting to access a service in a cross-domain way while the service is not configured for cross-domain access. You may need to contact the owner of the service to expose a sockets cross-domain policy over HTTP and host the service in the allowed sockets port range 4502-4534.
类型System.Net.Sockets的innerException。说
An attempt was made to access a socket in a way forbidden by its access permissions.
这些异常背后可能的原因是什么?根据我目前的调查,我只能找到一个原因:不当的 clientaccessppolicy .xml。还有其他原因吗?如果你有任何有用的资源,请提供给我。还有一个问题,如果我想让Windows服务托管WCF服务被LAN上的其他机器使用,我必须做什么设置?例如防火墙设置?我的代码无法访问其他机器上的WCF服务。它抛出与我上面提到的相同的异常。关于如何摆脱这个异常有什么想法吗?
问题排序…!!我必须做以下事情:
1)在Windows服务中创建NetTcpBinding
时指定了SecurityMode.None
2)在Windows高级安全防火墙中创建入站规则,允许TCP流量通过端点地址中指定的端口。