如何跨域支持添加到WCF服务 [英] How to add cross domain support to WCF service

查看:315
本文介绍了如何跨域支持添加到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让从我的本地主机托管的JavaScript应用程序的POST请求:80在不同的端口承载的WCF RESTful服务,但不知何故,这是行不通的。我尝试添加自定义属性的标题,以及在我服务的 JSONData 编程的方法添加它,但我仍然得到我的回应405不允许的方法 。什么是这里的正确方法?

这是我的接口:

 命名空间RestService
{
    公共类RestServiceImpl:IRestServiceImpl
    {
        #区域IRestServiceImpl成员        公共字符串JSONData()
        {
            HttpContext.Current.Response.AddHeader(访问控制允许原产地,*);
            返回你的POST请求;
        }        #endregion
    }
}

和服务code:

 使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.Web.Script.Services;命名空间RestService
{    [服务合同]
    公共接口IRestServiceImpl
    {
        [OperationContract的]
        [ScriptMethod]
        [WebInvoke(方法=POST
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate =出口)
        串JSONData();
    }
}

最后的配置:

 <?XML版本=1.0&GT?;
<结构>  <&的System.Web GT;
    <编译调试=真targetFramework =4.0/>
  < /system.web>
  < system.serviceModel>
    <服务和GT;
      <服务名称=RestService.RestServiceImplbehaviorConfiguration =ServiceBehaviour>
        <端点地址=绑定=的WebHttpBinding合同=RestService.IRestServiceImplbehaviorConfiguration =网络>
        < /端点>
      < /服务>
    < /服务>    <&行为GT;
      < serviceBehaviors>
        <行为NAME =ServiceBehaviour>
          < serviceMetadata httpGetEnabled =真/>
          < serviceDebug includeExceptionDetailInFaults =FALSE/>
        < /行为>
      < / serviceBehaviors>
      < endpointBehaviors>
        <行为NAME =网络>
          < webHttp />
        < /行为>
      < / endpointBehaviors>
    < /行为>
    < serviceHostingEnvironment multipleSiteBindingsEnabled =真/>
  < /system.serviceModel>
  < system.webServer>
    <模块runAllManagedModulesForAllRequests =真/>
    < httpProtocol>
      < customHeaders>
         <添加名称=访问控制允许来源VALUE =*/>
      < / customHeaders>
< / httpProtocol>
  < /system.webServer>< /结构>


解决方案

这比在Web.config版本为我工作好:

创建一个Global.asax中

将此code到Global.asax.cs中:

 保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
    HttpContext.Current.Response.AddHeader(访问控制允许原产地,*);
    如果(HttpContext.Current.Request.HttpMethod ==选项。)
    {
        HttpContext.Current.Response.AddHeader(访问控制允许的方法,GET,POST);
        HttpContext.Current.Response.AddHeader(访问控制允许报头,内容类型,接受);
        HttpContext.Current.Response.AddHeader(访问控制-max-age的,1728000);
        HttpContext.Current.Response.End();
    }
}

<一个href=\"http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html\">http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html

I'm trying to allow POST requests from my javascript app hosted at localhost:80 to a WCF REStful service hosted at a different port, but somehow it doesn't work. I've tried adding custom properties to the header, as well as adding it programatically in my service's JSONData method but I'm still getting '405 Method not allowed' in my response. What is the proper approach here ?

This is my interface :

namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestServiceImpl Members

        public string JSONData()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            return "Your POST request";
        }

        #endregion
    }
}

and the service code :

using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Script.Services;

namespace RestService
{

    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [ScriptMethod]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "export")]
        string JSONData();
    }
}

And finally the config :

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <httpProtocol>
      <customHeaders>
         <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
</httpProtocol>  
  </system.webServer>

</configuration>

解决方案

This worked better for me than the Web.config version:

Create a Global.asax

Add this code to the Global.asax.cs:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html

这篇关于如何跨域支持添加到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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