跨域异常与WcfSvcHost [英] Cross Domain Exception with WcfSvcHost

查看:97
本文介绍了跨域异常与WcfSvcHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和另一个跨域问题,很抱歉。



我一直在这一切现在一天战斗,是在沸腾了的地步。



我有一个Silverlight应用程序项目(SLApp1),Web项目主办的Silverlight(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> 
<服务和GT;
<服务名称=Test.Service1behaviorConfiguration =Test.Service1Behavior>
<主机>
< baseAddresses>
<添加baseAddress =HTTP://本地主机:8055/>
< / baseAddresses>
< /主机>
<端点地址=服务1绑定=basicHttpBinding的合同=Test.IService1/>
<端点地址=绑定=的WebHttpBindingbehaviorConfiguration =webHttpBehavior合同=Test.WCFService.IClientAccessPolicy/>
<端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
< /服务>
< /服务>
<&行为GT;
< serviceBehaviors>
<行为NAME =Test.WCFService.Service1Behavior>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =FALSE/>
< /行为>
< / serviceBehaviors>
< endpointBehaviors>
<行为NAME =webHttpBehavior>
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>
< /system.serviceModel>


解决方案

好吧不知何故,我得到它的工作使用REST和的WebHttpBinding招。我做了一个改变我如何读取读取只使用一个缺点字符串的文件中clientaccesspolicy



下面是我如何解决这个问题:



的App.config:

 < system.serviceModel> 
<服务和GT;
<服务名称=Symphony.Server.WCFService.Service1behaviorConfiguration =Symphony.Server.WCFService.Service1Behavior>
<主机>
< baseAddresses>
<添加baseAddress =HTTP://本地主机:8055/>
< / baseAddresses>
< /主机>
<端点地址=服务1绑定=basicHttpBinding的合同=Symphony.Server.WCFService.IService1/>
<端点地址=绑定=的WebHttpBindingbehaviorConfiguration =webHttpBehavior合同=Symphony.Server.WCFService.IClientAccessPolicy/>
<端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
< /服务>
< /服务>
<&行为GT;
< serviceBehaviors>
<行为NAME =Symphony.Server.WCFService.Service1Behavior>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =FALSE/>
< /行为>
< / serviceBehaviors>
< endpointBehaviors>
<行为NAME =webHttpBehavior>
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>





IClientAccessPolicy.cs

  [的ServiceContract] 
公共接口IClientAccessPolicy
{
[OperationContract的, WebGet(UriTemplate =/clientaccesspolicy.xml)]
流getPolicy方法();
}



Service1.cs



 公共类服务:IService1,IClientAccessPolicy 
{
公众的IList<串GT;的GetData(字符串值)
{
//一些逻辑
}

[OperationBehavior]
公共流getPolicy方法()
{
常量字符串结果= @< XML版本=1.0,编码=UTF-8>
<获得政策>
<跨域-access>
<政策>
<让 - 从HTTP请求报头=*>
<域URI =*/>
< /让 - 从>
<赠款至大于
<资源路径=/包括-子路径=真/>
< /赠款到大于
< /政策与GT;
< /跨域访问>
< /接入政策>中,

如果(WebOperationContext.Current!= NULL)
WebOperationContext.Current.OutgoingResponse.ContentType =application / xml进行;
返回新的MemoryStream(Encoding.UTF8.GetBytes(结果));
}

注意:
必须包括System.ServiceModel.Web作为参照



感谢您


and sorry for another Cross Domain question.

I have been fighting with this all day now and am at the point of boiling over.

I have a Silverlight Application Project (SLApp1), a Web project for hosting the Silverlight (SLApp1.Web) and the WCF Project (SLWcfService).

Now I am building everything together and all the projects are in one solution. The Web project is hosted by Visual Studio and the WCF Service is hosted by the WcfSvcHost.

The problem that because the web and wcf are hosted separately there is a cross domain issue, so every time my Silverlight application attempts to make a call to the WCF the Cross Domain Exception is thrown.

I have tried:

  1. Adding clientaccesspolicy.xml to C:\inetpud\wwwroot
  2. Using webHttpBinding and having a ReadPolicy method in the service

The problem is I am still developing the solution and so publishing the service and then hosting in IIS is simply not a feasible solution.

Why does this have to be so INCREDIBLY difficult?

Please Help!

Reference Material:

My 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>

解决方案

Ok somehow I got it working using the REST and webHttpBinding trick. I made a change to how I read the clientaccesspolicy from a file read to just using a cons string.

Here is how I resolved the problem:

App.config:

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

IClientAccessPolicy.cs

[ServiceContract]
public interface IClientAccessPolicy
{
    [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
    Stream GetPolicy();
}

Service1.cs

public class Service1 : IService1, IClientAccessPolicy
{
    public IList<string> GetData(string value)
    {
        //Some logic
    }

    [OperationBehavior]
    public Stream GetPolicy()
    {
        const string result = @"<?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>";

        if (WebOperationContext.Current != null)
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
        return new MemoryStream(Encoding.UTF8.GetBytes(result));
    }

Note: You must include System.ServiceModel.Web as a reference

Thank you

这篇关于跨域异常与WcfSvcHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆