【问题标题】:Silverlight client consuming WCF service throws System.ServiceModel.CommunicationException使用 WCF 服务的 Silverlight 客户端引发 System.ServiceModel.CommunicationException
【发布时间】:2014-08-22 19:48:25
【问题描述】:

请多多包涵,因为我是 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 不是罪魁祸首,那么这个异常背后的原因可能是什么?

【问题讨论】:

    标签: c# wcf silverlight nettcpbinding clientaccesspolicy.xml


    【解决方案1】:

    摆脱了异常..!!我在服务实现之前添加了以下属性:

        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    

    我在指定绑定时删除了安全模式,如下所示:

    var netTcpBinding = new NetTcpBinding(SecurityMode.None);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多