请耐心等待,因为我是 WCF 服务/Windows 服务的菜鸟。我创建了一个托管在 Windows 服务中的 WCF 服务。我想在 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;
}
}
我可以在其他本地非 Silverlight 应用程序中毫无例外地使用 WCF 服务。但在 Silverlight 中,它会引发异常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 添加以下标签:
<grant-to>
<socket-resource port="4502-4534" protocol="tcp" />
</grant-to>
不幸的是,没有一个解决方案对我有帮助。我想这个问题与 ClientAccessPolicy.xml 有关。我怀疑我错过了将 ClientAccessPolicy.xml 放在正确的位置。我将它保存在 WCF 服务的根目录中。这对 netTcpBinding 有用吗?如果 ClientAccessPolicy.xml 不是罪魁祸首,那么此异常背后的原因可能是什么?