WCF DataService not supporting preflight OPTIONS requests?

隐身守侯 提交于 2019-12-07 03:35:59

问题


I want to use an ajax-based component (KendoUI) to read/modify entities on an OData endpoint implemented by WCF DataServices.

The service implementation was fairly easy in the first place:

public class MyFooService : DataService<FooContext>
{
    public static void SetEntitySetAccessRules(IDataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Foos", EntitySetRights.AllWrite);
    }
}

Now I was expecting to be able to modify entities using PUT. KendoUI provides a nice and easy configuration interface and does a good job in generating the PUT request.

We are making a cross-domain request and use CORS. So, Firefox, for example, sends a preflight OPTIONS request to the OData service before sending the PUT.

Unfortunately the service endpoint seems not to support OPTIONS out-of-the-box: The response to the OPTIONS request is "501 Not Implemented" with an empty content. At least we managed that the response has the CORS headers as follows:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <!-- Enable cross-origin resource sharing -->
  <!-- http://enable-cors.org/#how-asp.net -->
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Methods" value="POST, PUT, DELETE, GET, OPTIONS" />
      <add name="Access-Control-Allow-Headers" value="content-Type, accept, origin, X-Requested-With" />
      <add name="Access-Control-Allow-Credentials" value="true" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Googling for this has turned out a bit challenging because "options" is a very popular term...

I found this article but it seems very, very complicated. I mean, OData is all about REST, I can't imagine that WCF Data Services don't provide a simple way to allowing preflight requests, or?


回答1:


Currently WCF DataServices don't support CORS, and every solution I have seen is a hack, and works flaky at best.

I had the same problem, and I just ported my code from WCF to an Web API 2 OData solution. Web API 2 has support for CORS and it is really easy to setup.

If you are familiar with Web API, check out this link: http://msdn.microsoft.com/en-us/magazine/dn532203.aspx

And here is a tutorial on how to create an OData endpoint with Web API: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/creating-an-odata-endpoint



来源:https://stackoverflow.com/questions/13516617/wcf-dataservice-not-supporting-preflight-options-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!