【问题标题】:CommunicationException occured due to the fact that my client access policy file was not accessed由于未访问我的客户端访问策略文件而发生 CommunicationException
【发布时间】:2011-05-13 14:25:53
【问题描述】:

我有一个托管在控制台应用程序中的 WCF 服务。
这是 app.config 文件:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CAVES.Framework.Network.IntegrationSuite.IntegrationServices.IntegrationServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="CrossDomainServiceBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="CAVES.Framework.Network.IntegrationSuite.IntegrationServices.IntegrationServiceBehavior"
        name="CAVES.Framework.Network.IntegrationSuite.IntegrationServices.IntegrationService">
        <endpoint address="" binding="basicHttpBinding" contract="CAVES.Framework.Network.IntegrationSuite.IntegrationServices.Interfaces.IIntegrationService">

        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/IntegrationService" />
          </baseAddresses>
        </host>
      </service>
      <service name="CAVES.Framework.Network.IntegrationSuite.IntegrationServices.CrossDomainService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="webHttpBinding" contract=
"CAVES.Framework.Network.IntegrationSuite.IntegrationServices.Interfaces.ICrossDomainService" behaviorConfiguration="CrossDomainServiceBehavior"/>
      </service>
    </services>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

当我使用套接字访问它时,客户端访问策略被正确获取,但是当我尝试调用 IntegrationService 的方法时,它给了我以下异常:

尝试制作时发生错误 对 URI 的请求 'http://localhost:8731/IntegrationService'。 这可能是由于试图 跨域访问服务 没有适当的跨域策略 已到位,或已制定的政策 不适合 SOAP 服务。你可以 需要联系楼主 发布跨域的服务 策略文件,并确保它允许 要发送的与 SOAP 相关的 HTTP 标头。 此错误也可能是由于使用 Web 服务中的内部类型 不使用代理 InternalsVisibleToAttribute 属性。 请参阅内部异常 更多细节。

内部异常:
{System.Security.SecurityException ---> System.Security.SecurityException: 安全错误。在 System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.BrowserHttpWebRequest.c_DisplayClass5.b_4(Object 发送状态)在 System.Net.Browser.AsyncHelper.c_DisplayClass4.b_1(Object sendState) --- 内部结束 异常堆栈跟踪 --- 在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback 开始方法,对象状态)在 System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult 结果)}

我的客户端访问策略文件看起来不错,它应该可以工作,但我不确定它为什么不工作。

<?xml version="1.0" encoding ="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from>
        <domain uri="*" />
      </allow-from>
      <grant-to>
        <socket-resource port="4502-4534" protocol="tcp" />
      </grant-to>
    </policy>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

【问题讨论】:

    标签: silverlight wcf clientaccesspolicy.xml


    【解决方案1】:

    当我使用 socket 访问它时, 获取客户端访问策略 正确但是当我尝试调用 IntegrationService 的方法它 给了我以下异常

    您的跨域策略文件仅允许从端口 4502 到 4534 的套接字连接,但您的应用程序正在尝试访问端口 8731。除非应用程序安装在浏览器之外(具有提升的权限),否则 Silverlight 只能访问此端口范围,所以 8731 不起作用。

    现在,您的服务正在使用 BasicHttpBinding,因此我假设您为此使用 HTTP。在这种情况下,策略文件必须与服务在同一个域中提供,并且它必须位于域根目录(在您的情况下为 http://localhost:8731/clientaccesspolicy.xml)。 http://blogs.msdn.com/b/carlosfigueira/archive/2008/03/07/enabling-cross-domain-calls-for-silverlight-apps-on-self-hosted-web-services.aspx 的博文提供了有关如何为自托管服务启用 SL 的跨域调用的更多信息。

    【讨论】:

    • 那么端口仅用于套接字并不重要吗?我相信政策是不同的。我的策略文件与根上的服务一起提供。但不在同一个端口上。唔。点采取,将检查。
    • 我为这两个 Web 服务使用了端口 80,现在它可以工作了。结论:始终在与服务端口相同的端口上提供 clientaccesspolicy.xml(对于套接字,http 上始终为 80,tcp 上始终为 943)。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多