如何为自托管WCF服务启用跨域调用

How to enable cross domain calls for self hosted WCF service
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我创建了一个在Windows计算机上运行的自托管WCF服务.现在,我想使用AJAX调用来使用该服务的操作.在iExplorer,Firefox和Chrome中,我都没有得到应有的结果JSON流.

为了允许跨域调用,我必须在自托管服务中添加什么?

问候西蒙

速聊1:
您使用什么绑定?WCF服务是如何托管的(进程?IIS?)
速聊2:
webHttpBinding,并使用ServiceBase.Run(new ioNotificationService())将其托管为Windows中的服务.
解决过程1

http://pranayamr.blogspot.se/2011/06/calling-cross-domain-wcf-service-using.html似乎是有效的文章.您需要相应地配置服务.过去这对我有用.

速聊1:
那篇文章建议添加[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)].从ASP,我假设本文引用的是Web托管服务.我的问题与自助服务有关.
速聊2:
有人知道自助服务的解决方案吗?
解决过程3

对于托管在localhost \ Service.mv​​c上的Service GetPerson,请使用:

$.ajax({
            type: 'GET', //GET or POST or PUT or DELETE verb
            url: 'Service.svc/GetPerson', // Location of the service
            data: userid, //Data sent to server
            contentType: 'application/json; charset=utf-8', // content type sent to server
            dataType: 'json', //Expected data format from server
            processdata: true, //True or False
            success: function (data) {//On Successfull service call
                alert(data.Name);
            },
            error: function (msg) {// When Service call fails
                alert(msg);
            }
        });

链接:http://www.cleancode.co.nz/blog/1041/complete-example-wcf-ajax-ssl-http

速聊1:
但这是否回答了有关跨域调用的问题?我不这么认为.如果此JQuery代码托管在与服务不同的域上,则此代码将失败,或者换句话说,您的浏览器将执行ajax请求,但不会接收任何数据.那是我的问题.
速聊2:
您可以从客户端计算机ping到服务器计算机吗?您可以解析名称(通常通过ping <computerName>.<DomainName>吗?)-如果是,请将该名称(<computerName>.<DomainName>)作为URL.
速聊3:
是的,我可以到达服务器,请求返回HTTP / 1.1 200 OK,但是没有接收到JSON流.
速聊4:
这似乎很奇怪.您确定要查看REST请求吗?我只在其余URL中使用完整的计算机名称,一切都很好.
速聊5:
非常感谢您的投入.当我在网址栏中输入Web服务的地址时,会得到一个JSON流,当我通过html页面发送ajax请求时,该页面不会收到任何JSON流.正如我所假设的,这是因为浏览器的跨域策略不允许向与调用JavaScript代码位于同一域的其他任何位置的Web服务发出Ajaw请求.对于IIS上的Web服务,我可以通过将文件添加到Web服务的根目录来解决此问题.但是对于作为Windows服务安装的自托管Web服务,我正在寻找新的答案.

I created a self hosted WCF service which runs on a windows machine. Now I would like to consume the service's operations using AJAX calls. Neither in iExplorer, Firefox nor Chrome I get a resulting JSON stream as supposed to.

What do I have to add to the self hosted service in order to allow cross domain calls?

Regards Simon

Talk1:
What binding do you use? How is the WCF service hosted (process? IIS?)
Talk2:
webHttpBinding, and it is hosted as a service in windows, using ServiceBase.Run(new ioNotificationService());
Solutions1

http://pranayamr.blogspot.se/2011/06/calling-cross-domain-wcf-service-using.html Seems like a valid article. You need to configure your service accordingly. This has worked for me in the past.

Talk1:
Well that article suggest to add [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]. From ASP I assume that the article refers to a web hosted service. My Problem concerns a self hosted service.
Talk2:
Does someone know the solution for a self hosted service?
Solutions3

For Service GetPerson hosted on localhost\Service.mvc use:

$.ajax({
            type: 'GET', //GET or POST or PUT or DELETE verb
            url: 'Service.svc/GetPerson', // Location of the service
            data: userid, //Data sent to server
            contentType: 'application/json; charset=utf-8', // content type sent to server
            dataType: 'json', //Expected data format from server
            processdata: true, //True or False
            success: function (data) {//On Successfull service call
                alert(data.Name);
            },
            error: function (msg) {// When Service call fails
                alert(msg);
            }
        });

Link: http://www.cleancode.co.nz/blog/1041/complete-example-wcf-ajax-ssl-http

Talk1:
But does this answer the question about cross Domain calls? I don't think so. If this JQuery code is hosted on a different Domain than the Service, this code will fail, or in other words, your browser will execute the ajax request but wont receive any data. That is my Problem.
Talk2:
Can you ping from client computer to the server computer? Can you resolve the name (usually by ping <computerName>.<DomainName> ? If so - put that (<computerName>.<DomainName>) as your URL.
Talk3:
Yes I can reach the server, the request returns HTTP/1.1 200 OK but no JSON stream is received.
Talk4:
This seems very strange. Are you sure you are looking at the REST request? I just use the full computer name in the rest URL, and everything is fine.
Talk5:
Thank you very much for your inputs. When I type in the adress of the webservice in the url bar, I get a JSON stream, when I sent a ajax request through an html page, the page does not receive any JSON stream. This is, as I assume, because of the cross domain policy by the browser not allowing ajaw requests to webservices located anywhere else then on the same domain as the calling javascript code. For my webservice on an IIS I could solve this issue by adding a file to the root of the webservice. But for a self hosted webservice installed as a windows service, I'm looking for a new answer.
转载于:https://stackoverflow.com/questions/19095973/how-to-enable-cross-domain-calls-for-self-hosted-wcf-service

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入