【问题标题】:TCP error code 10013 Cross Domain Policy Error with netTcpBinding Service and Silverlight ClientTCP 错误代码 10013 netTcpBinding 服务和 Silverlight 客户端的跨域策略错误
【发布时间】:2026-01-31 15:45:01
【问题描述】:

如果有人可以让我摆脱跨域策略的地狱,我很想知道。

我有一个使用netTcpBining 的双工 WCF 服务,并且客户端是 Silverlight 4 应用程序。当我自己托管该服务时,它可以完美运行,我的 Silverlight 客户端可以毫无问题地使用该服务。但是,当我在 IIS 7 中托管它时,问题就开始了。当我在 IIS 中托管它时,我可以在以下位置看到该服务:

http://localhost/Conference/VideoConferenceService.svc

当我添加对托管在 IIS 中的服务的引用并尝试调用它时,我得到一个:

CommunicationException:无法连接到 net.tcp://localhost/Conference/VideoConferenceService.svc。这 连接尝试持续了 00:00:03.3071892 的时间跨度。 TCP 错误代码10013:试图以某种方式访问​​套接字 被其访问权限禁止。这可能是由于尝试 以跨域方式访问服务,而服务不是 配置为跨域访问。您可能需要联系店主 通过 HTTP 公开套接字跨域策略的服务和 在允许的套接字端口范围 4502-4534 中托管服务。

或者,如果看到实际错误有助于激励那些以前看过它的人,那么它在Reference.cs 中向我抛出了以下内容:

我已经检查了几乎所有关于解决跨域策略错误的建议解决方案,并且我已经将我的 clientaccesspolicy.xml 放在了 IIS 的默认网站根目录中,也放在了 wwwroot 中。我还关闭了所有防火墙。我可以在http://localhost/clientaccesspolicy.xml 看到该政策 也在http://127.0.0.1/clientaccesspolicy.xml,但我仍然收到此错误。

这是我在 IIS 7 中托管的服务的 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <runtime>
    <gcServer enabled="true" />
  </runtime>

  <system.web>
    <compilation debug="false" />
  </system.web>
   <system.serviceModel>
     <services>
       <service name="VideoServer.VideoConferenceService">
         <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IVideoConferenceService" contract="VideoServer.IVideoConferenceService" />
         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
         <host>
           <baseAddresses>
                <add baseAddress="net.tcp://localhost:4502/VideoServer/" />
           </baseAddresses>
         </host>
       </service>
     </services>

    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IVideoConferenceService" portSharingEnabled="true" transactionFlow="false" transferMode="Buffered" listenBacklog="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="24.20:31:23.6470000" openTimeout="24.20:31:23.6470000" receiveTimeout="24.20:31:23.6470000" sendTimeout="24.20:31:23.6470000">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <reliableSession enabled="false" />
          <security mode="None">
            <message clientCredentialType="None" />
            <transport protectionLevel="None" clientCredentialType="None" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="False" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
    <system.webServer>
        <directoryBrowse enabled="true" />
    </system.webServer>

</configuration>

这是 Silverlight 客户端的 ServiceReferences.ClientConfig 文件:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="NetTcpBinding_IVideoConferenceService">
                    <binaryMessageEncoding />
                    <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost/Conference/VideoConferenceService.svc"
                binding="customBinding" bindingConfiguration="NetTcpBinding_IVideoConferenceService"
                contract="ServiceReference1.IVideoConferenceService" name="NetTcpBinding_IVideoConferenceService" />
        </client>
    </system.serviceModel>
</configuration>

有人有什么建议吗?到目前为止,这个烦人的错误已经花费了我几天的时间。 任何帮助将不胜感激。

编辑

当我使用 fiddler 检查在我的网络会话中检索到的文件时,它显示我的浏览器正在检索客户端访问策略文件,所以我认为错误出在其他地方,而 WCF 只是向我抛出了这个错误?我还设置了 IE9 以在每次关闭时清除其缓存。看看下面。

【问题讨论】:

  • 我会检查您的客户端访问策略是否正在被请求并且没有被缓存,我观察到这些策略文件可以被积极缓存。我会清除您的缓存并查看您的请求(使用 IE9 开发工具、Firebug、Fiddler 等中的网络监视器)以查看您的浏览器获取的客户端访问策略。
  • 谢谢,现在试试,看看会发生什么。
  • @Rus,我这样做了,但似乎并没有解决问题。我还将浏览器设置为始终清理其缓存。我更新了我的答案,如果你能看一下,不胜感激。
  • 顺便说一句,有趣的是,您说自托管对于 SL 客户端调用 WCF 服务是可以的。那你有没有配置clientaccesspolicy.xml?如果你没有配置它根本不应该工作。
  • 是的,显然我还必须为自托管服务配置客户端访问策略。我正在手动检索客户端访问策略。我在打开服务主机之前放置了一个断点以检查是否正在读取策略,并且在自托管环境中,每当创建客户端代理时总是会读取它。基本上我做了this

标签: wcf silverlight iis iis-7 nettcpbinding


【解决方案1】:

好吧,我只是设法让它工作。值得一提的是:

1.如果您查看我在上面发布的错误 ServiceReferences.ClientConfig(由 Visual Studio 在我将服务地址提供为:http://localhost/Conference/VideoConferenceService.svc 时生成),您可以看到4502的netTcp端口不是作为端点的一部分生成的,这就是导致TCP错误10016(EndpointNotFoundException)以及TCP错误10013的原因。正确的ServiceReferences.ClientConfig is实际上:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="NetTcpBinding_IVideoConferenceService">
                    <binaryMessageEncoding />
                    <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:4502/Conference/VideoConferenceService.svc"
                binding="customBinding" bindingConfiguration="NetTcpBinding_IVideoConferenceService"
                contract="ServiceReference1.IVideoConferenceService" name="NetTcpBinding_IVideoConferenceService" />
        </client>
    </system.serviceModel>
</configuration>

2. 当我在 IIS 7 中托管我的服务时,我将端口范围 808:* 作为 netTcp 端口,而我应该将 4502:* 作为端口范围,例如下面:

根据我收集到的信息,托管服务的网站应该在端口 80 上,因为 Silverlight 会在 localhost:80/ClientAccessPolicy.xml 中查找客户端访问策略文件。

为了记录,对于那些偶然发现同样问题的人,这个web.config 设法在 IIS 中工作:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="false" />
  </system.web>

   <system.serviceModel>
     <services>
       <service name="VideoServer.VideoConferenceService">
         <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IVideoConferenceService" contract="VideoServer.IVideoConferenceService" />
         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
       </service>
     </services>

    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IVideoConferenceService" portSharingEnabled="false" transactionFlow="false" transferMode="Buffered" listenBacklog="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="24.20:31:23.6470000" openTimeout="24.20:31:23.6470000" receiveTimeout="24.20:31:23.6470000" sendTimeout="24.20:31:23.6470000">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <reliableSession enabled="false" />
          <security mode="None">
            <message clientCredentialType="None" />
            <transport protectionLevel="None" clientCredentialType="None" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
    <system.webServer>
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>

另外值得注意的是,对于 IIS,您不需要在 web.config 中放置任何端口或基地址。它将使用端口 4502 创建服务端点,我尝试将其更改为 4503、4522 等,有趣的是它不适用于这些端口,仅适用于 4502。

【讨论】:

    【解决方案2】:

    我们遇到了与原始帖子中所述类似的问题。只是为了帮助无法使用上述解决方案解决问题的任何人,我们发现托管该服务的计算机上的 Windows 防火墙阻止了我们尝试通过 net.tcp 连接的端口。一旦我们允许流量通过该软件防火墙上的端口,我们的服务就会开始按预期工作。

    可能值得检查您可能拥有的任何硬件防火墙,以允许我相信 net.tcp 的范围为 4502-4530

    【讨论】: