WCF Rest With Jquery Ajax 和 .Net 应用咨询
我需要您的帮助和咨询来解决我因一些错误的架构应用程序设计而遇到的问题。
案例:我正在为客户端构建基于asp.NET 4.0的Web应用程序,我几乎已经完成了该项目的85%,项目架构如下:
- 前端服务器:Web应用程序,Ajax JQuery调用WCF Rest服务。
- 应用程序服务器:托管 WCF Rest 服务和 DAL 组件。
- SQL Server:自行托管数据库
我认为问题在于跨域问题,我的客户确保解决方案应部署在 3 台服务器上,正如您所知,我已经坚持使用解决方案,并且我需要找到一个退出我的问题。所有 3 个服务器都托管在同一域,但具有不同的 IP 地址,我尝试使用 JSONP 方法,但它似乎有一些限制和缺点,例如没有错误处理,这是我的客户要求之一(是否可以进行 POST,使用 JSONP 进行 PUT 操作?),我还尝试维护服务器响应标头,但我认为它不适用于所有浏览器,例如 IE 9、Chrome ..
这个问题确实让我处于非常尴尬的境地,我需要您的专业和咨询帮助,有些人建议我创建另一个在前端托管的 WCF 休息,它调用其他服务,但我认为这会使性能非常慢。
干杯。
I need your help and consultation in a problem that i have faced based on some wrong architecture application design.
The case: im working in web application built on asp.NET 4.0 to a client and im almost have done 85% out of this project, the project architecture as the following:
- Front End Server: Web Application, Ajax JQuery Calling WCF Rest Service.
- Application Server: Host the WCF Rest Service and the DAL components.
- SQL Server: host the database it self
My problem i think is with the CROSS-DOMAIN issue, and my client ensure that the solution should be deployed on the 3 servers, as u know i have already stucked with solution and i need to find an exit to my problem. all the 3 servers are hosted at the same DOMAIN but with different ip address, i tried to go with JSONP approach but it seems have some limitations and drawbacks such as no error handling which is one my client requirement (is it possible to make POST, PUT operations using JSONP ?), also i tried to maintain the server response headers but i think its not going to work with all browsers such as IE 9, Chrome ..
This problem really puts me in a very embarrassing situation and i need your professional and consultation help, some people suggested that i create another WCF rest hosted in the frontend and it calls the other service but i think it will make the performance very slow.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也一直在与这个问题作斗争,但尚未找到完美的答案。这里有一些事情至少可以让你在路上走得更远。
一种选择是配置 webHttp 行为,使其不包含 failureExceptionEnabled="true" 属性。这样,jQuery 不会触发错误处理程序,而是触发成功处理程序。 WebFaultException 仍应使用 JSON 填充成功处理程序中的数据参数。这种方法的缺点是我还没有找到一种方法来包装错误异常,因此我可以轻松检查该成功处理程序中是否确实发生了错误。我在 JSON 中得到的只是故障的属性。
下一个选项是将 Web 前端放入与 WCF 服务相同的网站上的不同虚拟目录中。这种方法不应该有任何跨域问题。并不总是理想的解决方案,但它确实有效。
如果我使用 Cassini Web 服务器或 IIS Express,我也会在本地遇到问题。我认为浏览器将对不同端口上的网站的请求解释为跨域请求。我可以在本地成功工作的唯一方法是在我的开发箱上安装 IIS,并在默认网站上托管服务和前端。
如果有人想出了更好的方法,请分享。这足以让一个人发疯。
I have been battling this issue too, and have not found a perfect answer yet. Here are some things that will at least get you further down the road.
One option is to configure your webHttp behavior so that it doesn't include the faultExceptionEnabled="true" attribute. This way, jQuery won't hit the error handler, but the success handler. The WebFaultException should still populate the data parameter in the success handler with JSON. The drawback to this approach is that I haven't found a way to wrap the fault exception so I can easily check if an error actually occurred in that success handler. All I get in the JSON are the properties of the fault.
The next option is just to drop the web front end into a different virtual directory on the same web site as the WCF service. There shouldn't be any cross-domain issues with this approach. Not always an ideal solution, but it works.
I had issues locally as well if I use the Cassini web server or IIS Express. I think the browser interprets requests to web sites on different ports as a cross-domain request. The only way I could work locally with any success is if I installed IIS on my dev box and hosted both the service and the front end on the default web site.
If anybody has figured out a better way, please share. It's enough to drive a guy bonkers.