简体   繁体   English

WCF DataService(OData)和CORS

[英]WCF DataService (OData) and CORS

I am trying to get a WCF DataService working with cross domain requests. 我正在尝试使用跨域请求的WCF DataService。 I found this on how to get a WCF service to work with CORS: http://blogs.microsoft.co.il/blogs/idof/archive/2011/07/02/cross-origin-resource-sharing-cors-and-wcf.aspx 我发现了如何让WCF服务与CORS一起工作: http//blogs.microsoft.co.il/blogs/idof/archive/2011/07/02/cross-origin-resource-sharing-cors-and -wcf.aspx

I downloaded the sample, but can't get it to work with a DataService. 我下载了示例,但无法使用DataService。 It works with the sample service, but not with my DataService. 它适用于示例服务,但不适用于我的DataService。

This is my very simple WCF DataService: 这是我非常简单的WCF DataService:

public class TestService : DataService<DataContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.UseVerboseErrors = true;
        config.SetEntitySetAccessRule("Items", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
} 

The TestService.svc file: TestService.svc文件:

<%@ ServiceHost Language="C#" Factory="WebHttpCors.CorsWebServiceHostFactory, WebHttpCors" Service="MvcApplication1.TestService" %>

The DataContext is also very simple: DataContext也很简单:

public class DataContext : DbContext
{
    public DbSet<Item> Items { get; set; }
}

But still, the preflight options request returns with a 501. Is there something I am missing to get CORS to work with a Dataservice? 但是,预检选项请求返回的是501.我是否缺少让CORS与Dataservice一起工作?

If you're using IIS, verify that the ExtensionLess handler is configured to handle the OPTIONS requests. 如果您使用的是IIS,请验证ExtensionLess处理程序是否已配置为处理OPTIONS请求。

A few notes unrelated to your direct issue: since CORS is not properly supported , neither the package you found nor any other solutions will be truly satisfactory (you won't be able to easily specify your policies). 与您的直接问题无关的一些注意事项:由于CORS 未得到适当支持 ,因此您找到的软件包或任何其他解决方案都不会真正令人满意(您将无法轻松指定您的策略)。 It's possible to create a professionally-maintained package to do this using WCF inspectors, but I haven't seen any. 可以使用WCF检查员创建一个专业维护的包,但我还没有看到。 Instead, I'd like to invite you to vote this up should you agree. 相反,如果您同意,我想邀请您投票

In the meantime, I can only recommend that you integrate any code you find on the web very carefully (as most of it is barely tested). 与此同时,我只能建议您非常仔细地集成您在网络上找到的任何代码(因为大部分代码都没有经过测试)。 This article may assist you with that. 本文可能会帮助您。 This is not directly related to Data Services, but it's the same WCF tech. 这与数据服务没有直接关系,但它与WCF技术相同。 Maybe look at the Web API implementation or other projects for inspiration. 也许可以看一下Web API实现或其他项目的灵感。

Good luck. 祝好运。

PS: In 90% of the situations, you'll also want to forget about solutions involving proxying . PS:在90%的情况下,您还想忘记涉及代理的解决方案。 In most architectures, it's just horrible and makes very little sense unless your edge backend is designed in a way that somehow would make it seem less kludgy. 大多数架构中,它只是非常糟糕并且没什么意义,除非你的边缘后端的设计方式不会让它看起来不那么笨拙。

Update: Also verify that the implementation you're using actually handles the OPTIONS requests properly. 更新:还要验证您正在使用的实现是否正确处理OPTIONS请求。 If it passes them through, WCF Data Services will return a 501, and the interceptor might just pass it back through as well even though the headers were set correctly. 如果它通过它们,WCF数据服务将返回501,即使标题设置正确,拦截器也可能只是将其传回。 Since preflight requests don't need a body, a quick and dirty hack would be to pickup these 501s and change them into 200s, but obviously you really want to stop the request from hitting the data service in the first place. 由于预检请求不需要正文,快速而肮脏的黑客就是拾取这些501并将其更改为200秒,但显然你真的想要阻止请求首先点击数据服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM