【发布时间】:2014-07-03 08:57:51
【问题描述】:
由于我是 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.SocketException类型的innerException说
An attempt was made to access a socket in a way forbidden by its access permissions.
这些异常背后的可能原因是什么?根据我目前的调查,我只能找到一个原因:不正确的 ClientAccessPolicy.xml。其他原因可能是什么?如果您有任何有用的资源,请提供给我。还有一个问题,如果我想让 Windows 服务托管的 WCF 服务被 LAN 上的其他机器使用,我必须进行哪些设置?例如。防火墙设置?我的代码无法访问其他机器上的 WCF 服务。它引发了我上面提到的相同异常。有关如何摆脱此异常的任何想法?
【问题讨论】:
-
除此链接:msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx 我无法帮助您解决错误,但应将防火墙配置为允许 Windows 服务可执行文件的传入请求,或者,如果您不打算更改服务端口,以允许该特定端口上的传入请求用于 tcp 流量。
-
感谢您的建议@Andreas..!!
标签: c# wcf silverlight nettcpbinding clientaccesspolicy.xml