c# - WcfSvcHost 的跨域异常

标签 c# wcf silverlight

抱歉还有一个跨域问题。

我现在一整天都在与它抗争,快要沸腾了。

我有一个 Silverlight 应用程序项目 (SLApp1)、一个用于托管 Silverlight 的 Web 项目 (SLApp1.Web) 和一个 WCF 项目 (SLWcfService)。

现在我正在一起构建所有内容,所有项目都在一个解决方案中。 Web 项目由 Visual Studio 托管,WCF 服务由 WcfSvcHost 托管。

问题是因为 web 和 wcf 是分开托管的,所以存在跨域问题,所以每次我的 Silverlight 应用程序尝试调用 WCF 时都会抛出跨域异常。

我试过:

  1. 将 clientaccesspolicy.xml 添加到 C:\inetpud\wwwroot
  2. 在服务中使用 webHttpBinding 和 ReadPolicy 方法

问题是我仍在开发解决方案,因此发布服务然后在 IIS 中托管根本不是一个可行的解决方案。

为什么这必须如此困难?

请帮忙!

引用资料:

我的 App.config:

     <system.serviceModel>
    <services>
      <service name="Test.Service1" behaviorConfiguration="Test.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8055" />
          </baseAddresses>
        </host>
        <endpoint address="Service1" binding="basicHttpBinding" contract="Test.IService1"/>
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webHttpBehavior" contract="Test.WCFService.IClientAccessPolicy" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Test.WCFService.Service1Behavior">
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

最佳答案

虽然我不确定这是否会解决您的问题,但我会向您展示我是如何设法修复类似的解决方案布局的,也许这些步骤会提供一些指导。

我使用 silverlight 项目 (domainexception)、web 项目 (domainexception.Web) 和 wcf 服务 (WcfServiceLibrary1) 设置了一个解决方案。我将 Service1 端点设为 basicHttpBinding,就像它在您的 App.config 中一样。

然后我添加了对 silverlight 项目的服务引用,并使用以下地址构建它:

"http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/"

尝试像这样运行它时我也遇到了跨域策略问题。

然后我向其中添加了 webHttpBinding 和方法,但仍然发现它不起作用。

所以,最后我把基地址从上面的改成了

http://localhost:8731

它奏效了。

最终成功了。这就是最后的样子。

App.config

 <system.serviceModel>
<services>
  <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">
    <host>
      <baseAddresses>
        <add baseAddress = "http://localhost:8731/" />
      </baseAddresses>
    </host>
    <endpoint address ="Service" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="policyBehavior" contract="WcfServiceLibrary1.IService1"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="policyBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="WcfServiceLibrary1.Service1Behavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>

ServiceReferences.ClientConfig

<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8731/Service" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

我的政策网络呼吁:

界面

[OperationContract, WebGet(UriTemplate="clientaccesspolicy.xml")]
    Stream ReturnPolicy();

代码

public System.IO.Stream ReturnPolicy()
    {
        string file = @"<?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>";
        return new MemoryStream(Encoding.UTF8.GetBytes(file));
    }

关于c# - WcfSvcHost 的跨域异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2326331/

相关文章:

c# - 填充 asp :dropdownlist - VS 2008

asp.net-mvc - Silverlight 4 - MVC 2 ASP.NET 成员(member)集成 "single sign on"

c# - WP7 - 在 ViewModel 中使用 Messenger.Default.Send(pageuri)。无需在任何地方注册此消息,导航仍然可以工作。如何?

c# - Silveright 验证在修复验证错误后抛出异常

c# - 了解 WCF ServiceBehaviorProperty ConcurrencyMode

c# - 对 3 个数字进行无分支排序

c# - 代码优先的实体约束 "check"

c# - 具有斯洛文尼亚文化的日期时间 (sl-SI) 有空格

wcf - 可以同时以JSON(P)和XML响应并且仍用作SOAP Web服务的RESTful WCF服务吗?

WCF:更改 ClientCredentials 会产生 "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed."