CORS 启用 WCF REST 和 Kendo
Posted
技术标签:
【中文标题】CORS 启用 WCF REST 和 Kendo【英文标题】:CORS enabled WCF REST and Kendo 【发布时间】:2014-03-03 08:52:04 【问题描述】:我在 wcf 休息服务部门工作。我需要允许 cors supprt 进行跨域访问。我读了文章,得到了2个方法。
第一种方法是在 global.asax 中的 application_beginrequest 事件中添加 http 标头。这对我来说很好。我使用 jquery 使用剑道图表绑定对此进行了测试。图表在 IE、Chrome 和 Firefox 中填充。在 global.asax 中启用 cors 的工作代码是
protected void Application_BeginRequest(object sender, EventArgs e)
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
但我需要配置 cors enabled 属性,所以我遵循了这个 link 。我复制并运行了成功的服务。但是在我在端点中启用此行为后,客户端没有在 Chrome 和 Firefox 中显示图表。所以没有启用跨域。我对吗?我想念这里。
我的新服务类是;
public class CorsEnabledBehavior : BehaviorExtensionElement, IEndpointBehavior
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
var requiredHeaders = new Dictionary<string, string>();
requiredHeaders.Add("Access-Control-Allow-Origin", "*");
requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CorsEnabledMessageInspector(requiredHeaders));
public void Validate(ServiceEndpoint endpoint)
public override Type BehaviorType
get return typeof(CorsEnabledBehavior);
protected override object CreateBehavior()
return new CorsEnabledBehavior();
public class CorsEnabledMessageInspector : IDispatchMessageInspector
Dictionary<string, string> requiredHeaders;
public CorsEnabledMessageInspector(Dictionary<string, string> headers)
requiredHeaders = headers ?? new Dictionary<string, string>();
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
return null;
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
foreach (var item in requiredHeaders)
httpHeader.Headers.Add(item.Key, item.Value);
我的网络配置是
<extensions>
<behaviorExtensions>
<add name="corsEnabledBehaviour" type="LAMI.Service.Utilities.CorsEnabledBehavior, LAMI.Service.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="endBehaviour1">
<webHttp helpEnabled="true" />
<corsEnabledBehaviour />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehaviour1">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpConfiguration" >
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="serviceBehaviour1" name="LAMI.Service.Service1">
<endpoint address="" behaviorConfiguration="endBehaviour1" binding="webHttpBinding"
bindingConfiguration="webHttpConfiguration" contract="LAMI.Service.Contract.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://ltms0/ServiceApp/Service1/" />
</baseAddresses>
</host>
</service>
</services>
你能帮我解决我想念的地方吗?
【问题讨论】:
我想为你投票 10,000 次。我已经尝试了一切 - 配置文件、自定义 IDispatchMessageInspector 以添加标题(您在 asax 中添加的相同标题/值) - 'GET' 一直在工作,但其他一切都失败了。将其移至 .asax 即可解决,非常感谢! 请帮助我:- ***.com/questions/27422063/… 【参考方案1】:我完成了这个。我使用 global.asax 启用了 cors 并成功运行。问题是iecors.js。这无法在 IE 8 和 9 中启用 cors。所有其他浏览器都可以正常工作
【讨论】:
以上是关于CORS 启用 WCF REST 和 Kendo的主要内容,如果未能解决你的问题,请参考以下文章
CORS(跨源资源共享)https 不工作(IIS 托管 WCF 休息启用端点)
启用从 jQuery ajax 到不在 IIS 中托管的 WCF 服务的 CORS POST