配置Breeze Web客户端以通过CORS支持连接到远程breeze服务器

时间:2014-07-25 13:34:53

标签: angularjs cors breeze breeze-sharp

我们正在设想一种产品,它将在多个平台(Xamarin)上拥有Web前端和移动应用程序。我已经把微风角毛巾的例子变成了网络前端。我的任务是调查拆分breeze web客户端和breeze服务器后端。其主要原因是移动设备可能会使用微风将其物体保存到相同的微风后端。让breeze Web客户端和服务器紧密耦合似乎是一个坏主意。我复制了项目并剥去了每一端的必要部件以将它们分离。

我无法弄清楚的部分是如何让他们再次相互交谈。我简要介绍了connectionString,但这似乎并不是正确的答案。关于如何让他们再次交谈的任何想法都将不胜感激。


编辑:20140725 14:23

我从昨天起就一直试图解决这个问题。我查看了Web.config中的connectionStrings,发现那是死路一条。另一篇帖子让我觉得appSettings在Web.config中。

我在config.js中找到了一个名为remoteServiceName的参数。之前的价值是" breeze / Breeze"我把它改成了

'http://localhost:4545/breeze/Breeze' 

网络客户端仍然失败:

Error retrieving data.Metadata query failed for: http://localhost:4545/breeze/Breeze/Metadata; HTTP response status 0 and no message. Likely did not or could not reach server. Is the server running? Error: Metadata query failed for: 'http://localhost:4545/breeze/Breeze/Metadata'; HTTP response status 0 and no message. Likely did not or could not reach server. Is the server running?'

当我在自己的标签中运行该链接时,我会获得元数据。


我删除了什么?:breeze控制器,模型,存储库(仅限c#),dbcontext和BreezeWebApiConfig


编辑20140725 14:52

对不起,我错过了上述例外之前的例外情况:

XMLHttpRequest cannot load http://localhost:4545/breeze/Breeze/Metadata. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:53555' is therefore not allowed access. 

1 个答案:

答案 0 :(得分:1)

感谢大家提供的评论中提供的线索!

我在上面的编辑中已经包含答案的第一部分:

  

我在config.js中找到了一个名为remoteServiceName的参数。之前的值是“breeze / Breeze”我将其更改为

'http://localhost:4545/breeze/Breeze' 

让客户端说话试图与远程服务器通话。

之后CORS问题困扰了我。以下三个链接帮助我解决了这个问题:

  

Using Breeze with a WebApi Service from another domain   WebAPI CORS and Ninject   http://msdn.microsoft.com/en-us/magazine/dn532203.aspx

您必须安装CORS包。来自微软的文章:

  

首先,为了获得CORS框架,您必须从Web API应用程序引用CORS库(默认情况下,它们不会从Visual Studio 2013中的任何Web API模板引用)。 Web API CORS框架可通过NuGet作为Microsoft.AspNet.WebApi.Cors包获得。如果你没有使用NuGet,它也可以作为Visual Studio 2013的一部分使用,你需要引用两个程序集:System.Web.Http.Cors.dll和System.Web.Cors.dll(在我的机器上这些位于C:\ Program Files(x86)\ Microsoft ASP.NET \ ASP.NET Web Stack 5 \ Packages)。

下一步是从webapi-cors-and-ninject stackoverflow帖子中为配置文件添加几行:

 <system.webServer>
<handlers>
</handlers>
<httpProtocol>
  <customHeaders>
    <!-- Adding the following custom HttpHeader will help prevent CORS from stopping the Request-->
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>

显然,这是一种非常不安全的解决方案,不应该用于开发环境以外的任何其他方面。

相关问题