Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF DataService not supporting preflight OPTIONS requests?

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?

like image 230
chiccodoro Avatar asked Nov 22 '12 16:11

chiccodoro


1 Answers

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

like image 147
Malyngo Avatar answered Oct 25 '22 11:10

Malyngo