【发布时间】: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