在Silverlight项目中将clientaccesspolicy.xml放在何处

sti*_*k81 3 .net silverlight wcf

我在客户端上有一个Silverlight应用程序通过WCF与服务器端通信.我偶尔会收到一个CommunicationException - 特别是在将大量数据传递给服务的某些参数时.有人告诉我,如果我希望Silverlight应用程序与这样的服务器通信,我需要一个clientaccesspolicy.xml文件 - 以防止跨站点脚本.

所以 - 我创建了clientaccesspolicy.xml默认值,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
  <cross-domain-access>
    <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>
Run Code Online (Sandbox Code Playgroud)

但现在我不知道该把它放在哪里.我需要告诉任何人它在哪里吗?而且 - 这可能是解决我的问题的方法吗?

Joh*_*nes 6

是的,当您希望Silverlight与外部源通信时,您需要clientaccesspolicy.xml.

您没有指定WCF服务是作为服务托管,是自托管还是托管在IIS中.如果它在IIS中,则该文件将放在共享文件夹(网站)的根目录中.

如果该服务是自托管的,那么您可以阅读本文.

对于Windows服务,您可以参考以下文章

但如果你偶尔得到错误,那么它可能不是你最大的问题.查看当您尝试发送大型参数时发生错误的事实,这意味着您必须查看WCF服务和客户端的绑定.这些限制通常类似于每次呼叫16kb.这可以在服务端完成,方法是创建一个允许传递大量数据的绑定.

<basicHttpBinding>
        <binding name="NewBinding0" maxBufferSize="104857600" maxReceivedMessageSize="104857600">
          <readerQuotas maxDepth="104857600" maxStringContentLength="104857600"
            maxArrayLength="104857600" maxBytesPerRead="104857600" maxNameTableCharCount="104857600" />
        </binding>
      </basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

然后将其关联到端点.

如果查看客户端的ServiceReferences.ClienConfig文件,您应该看到绑定到WCF服务.

你可以编辑它看起来像:

<binding name="ProductConfig" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                  <security mode="None" />
                </binding>
Run Code Online (Sandbox Code Playgroud)

编辑:这是如何在服务器端添加绑定.

  • 右键单击web.config并说"编辑WCF配置"
  • 在右边,有一个树元素"绑定"
  • 右键单击它并说"添加新绑定"
  • 命名绑定并将所有Max*元素设置为任意大的数字.
  • 通过扩展服务并选择正在使用的端点,将此绑定与您的端点相关联.在BindingConfiguration中,选择新绑定.

您也可以通过查找元素手动将其添加到web.config文件中

<system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

那里应该有一个<bindings>子元素.您可以在其中添加如上所示的绑定.然后向下滚动到显示端点的位置,并在xml中添加bindingConfiguration ="NewBinding0"标记.

编辑2:

好的,当然,这是我的一个项目中的示例:

<system.serviceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <extensions>

    </extensions>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0" maxBufferSize="104857600" maxReceivedMessageSize="104857600">
          <readerQuotas maxDepth="104857600" maxStringContentLength="104857600"
            maxArrayLength="104857600" maxBytesPerRead="104857600" maxNameTableCharCount="104857600" />
        </binding>
      </basicHttpBinding>
      <mexHttpBinding>
        <binding name="NewBinding1" />
      </mexHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="policyBehavior">
          <webHttp />
        </behavior>
        <behavior name="NewBehavior" />
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="NewBehavior" name="ALMWCFHost.ServiceModel">
        <clear />
        <endpoint address="GuildService" binding="basicHttpBinding" bindingConfiguration="NewBinding0"
          name="ProductConfig" contract="ALMWCFHost.IProductConfigModel"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://omrsrv004/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

如果您遇到更多问题,请提供您正在使用的IDE的详细信息,以及您最初是如何添加服务端点的.