WCF错误:405不允许的方法 [英] WCF error : 405 Method Not Allowed
问题描述
要疯了这个问题。我有2个项目的解决方案,其中之一是一个普通的旧的HTML使用jQuery ajax调用,而另一个是一个WCF服务。 HTML页面会发出一个Ajax调用WCF服务得到一个JSON字符串,并将其用于显示的目的。
现在的问题是,每当我在调试模式下运行,HTML页面和WCF将与不同的端口启动。这创造了一个跨域的问题对我来说,当我执行测试(即获得405不允许的方法误差与调用类型在Firefox =选项)。我三倍检查我的AJAX脚本调用方法和WCF服务是相同的(GET)。
我要搜索谷歌,但发现任我必须安装一个扩展或在IIS上,我发现很麻烦,因为我在做什么是简单的东西进行一些配置。下面的一个例子,我要补充在我的web.config以下配置,但它没有工作:
< system.serviceModel>
<&绑定GT;
<&的WebHttpBinding GT;
<绑定名称=跨域crossDomainScriptAccessEnabled =真/>
< /&的WebHttpBinding GT;
< /绑定>
<&行为GT;
< endpointBehaviors>
<行为NAME =MobileService.webHttpBehavior>
< webHttp />
< /行为>
< / endpointBehaviors>
< serviceBehaviors>
<行为NAME =MyServiceBehavior>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =真/>
< /行为>
< / serviceBehaviors>
< /行为>
< serviceHostingEnvironment aspNetCompatibilityEnabled =真multipleSiteBindingsEnabled =真/>
<服务和GT;
<服务名称=MobileService.SimpleMemberInfobehaviorConfiguration =MyServiceBehavior>
<端点地址=绑定=的WebHttpBinding合同=MobileService.IMemberInfobindingConfiguration =跨域behaviorConfiguration =MobileService.webHttpBehavior>
< /端点>
< /服务>
< /服务>
< /system.serviceModel>
< system.webServer>
< httpProtocol>
< customHeaders>
<添加名称=访问控制允许来源VALUE =*/>
<添加名称=访问控制允许的方法VALUE =GET/>
<添加名称=访问控制 - 允许 - 头VALUE =Content-Type的,接受/>
< / customHeaders>
< / httpProtocol>
<模块runAllManagedModulesForAllRequests =真/>
< directoryBrowse启用=真/>
< /system.webServer>
任何人有任何想法摆脱这恼人的问题呢?
编辑:我想补充,我正在与IIS防爆preSS与一起而来的调试VS工作室2012
添加在WCF code和更新的web.config
[的ServiceContract]
公共接口IMemberInfo
{
[WebInvoke(方法=GET,
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json
)]
[OperationContract的]
串GetMemberInfoById();
// TODO:添加您的业务运营在这里
}
我的脚本:
$(文件)。就绪(函数(){
$阿贾克斯(HTTP://本地主机:32972 / SimpleMemberInfo.svc / GetMemberInfoById,{
缓存:假的,
beforeSend:功能(XHR){
$ .mobile.showPageLoadingMsg();
},
完成:功能(){
$ .mobile.hidePageLoadingMsg();
},
的contentType:应用/ JSON,
数据类型:JSON,
输入:GET,
错误:函数(){
警报('可怕的事情发生了');
},
成功:功能(数据){
变种S =; S + =<立GT; +数据+< /李>中;
$(#myList上)HTML(S); }
});
});
您需要使用JSONP进行跨域调用避开浏览器限制,并与 crossDomainScriptAccessEnabled来更新你的web.config
设置为true,避开服务器的。有在这里回答一个很好的例子:<一href=\"http://stackoverflow.com/questions/5686059/how-to-avoid-cross-domain-policy-in-jquery-ajax-for-consuming-wcf-service\">how为了避免在jQuery的AJAX跨域策略为消费WCF服务?
您还可能有GET请求的一个问题。尝试此处列出的修补程序:
<一href=\"http://stackoverflow.com/questions/944678/making-a-wcf-web-service-work-with-get-requests\">Making用GET请求一个WCF Web服务工作
总之,你想要一个web.config,看起来是这样的:
&LT;&绑定GT;
&LT;&的WebHttpBinding GT;
&LT;绑定名称=跨域crossDomainScriptAccessEnabled =真/&GT;
&LT; /&的WebHttpBinding GT;
&LT; /绑定&GT;
&LT;&行为GT;
&LT; endpointBehavior&GT;
&LT;行为NAME =restBehavior&GT;
&LT; webHttp /&GT;
&LT; /行为&GT;
&LT; / endpointBehavior&GT;
&LT; serviceBehavior&GT;
&LT;行为NAME =MyServiceBehavior&GT;
&LT; serviceMetadata httpGetEnabled =真/&GT;
&LT; serviceDebug includeExceptionDetailInFaults =真/&GT;
&LT; /行为&GT;
&LT; / serviceBehavior&GT;
&LT; /行为&GT;
&LT;服务和GT;
&LT;服务名称=...behaviorConfiguration =MyServiceBehavior&GT;
&LT;端点地址=绑定=的WebHttpBindingbindingConfiguration =跨域
合同=...behaviorConfigurations =restBehavior/&GT;
&LT; /服务&GT;
&LT; /服务&GT;
(请注意,无论是服务和终端都连接行为,让webHttp电话和HTTPGET分别调用,并绑定了跨域访问明确启用)。
...一个服务方法装饰是这样的:
[的ServiceContract]
公共接口IMyService
{
[WebGet] //必须的属性,让GET
[OperationContract的]
字符串的MyMethod(字符串MyParam);
}
...并使用JSONP客户端调用:
&LT;脚本类型=文/ JavaScript的&GT;
$(文件)。就绪(函数(){
VAR URL =...;
$ .getJSON(URL +?回调=?,空,功能(结果){//注意的关键?回调=?
//处理结果
});
});
&LT; / SCRIPT&GT;
Going nuts with this issue. I have a solution with 2 projects, one of them is a plain old html with jquery ajax call while the other is a WCF service. The html page will issue a ajax call to the WCF service to get a json string and use it for display purpose.
Now the issue is whenever i run in debug mode, both the html page and the WCF will be started with different port. And this has created a cross-origin issue for me when i perform testing (i.e. getting a 405 Method Not Allowed error with the calling type = OPTIONS in Firefox). I'd triple check the call method on my ajax script and the WCF service is the same (GET).
I'd search google but found that either i have to install a extension or perform some configuration on IIS, which i found cumbersome since what i'm doing is something simple. Following one example, I'd add in the following configuration in my web.config but it didn't work:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="MobileService.webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="MobileService.SimpleMemberInfo" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="MobileService.IMemberInfo" bindingConfiguration="crossDomain" behaviorConfiguration="MobileService.webHttpBehavior">
</endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
Any one has any idea to get rid of this annoying issue?
EDIT: Just to add, I'm running the debug with IIS Express that comes together with the VS Studio 2012
Add in WCF Code and Updated web.config
[ServiceContract]
public interface IMemberInfo
{
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json
)]
[OperationContract]
string GetMemberInfoById();
// TODO: Add your service operations here
}
My Script:
$(document).ready(function () {
$.ajax("http://localhost:32972/SimpleMemberInfo.svc/GetMemberInfoById", {
cache: false,
beforeSend: function (xhr) {
$.mobile.showPageLoadingMsg();
},
complete: function () {
$.mobile.hidePageLoadingMsg();
},
contentType: 'application/json',
dataType: 'json',
type: 'GET',
error: function () {
alert('Something awful happened');
},
success: function (data) {
var s = "";
s += "<li>" + data + "</li>";
$("#myList").html(s);
}
});
});
You need to use JSONP for a cross-domain call to get round the browser restrictions, and to update your web.config with crossDomainScriptAccessEnabled
set to true to get round server ones. There's a good example in the answer here: how to avoid cross domain policy in jquery ajax for consuming wcf service?
You may also have a problem with GET requests. Try the fixes outlined here: Making a WCF Web Service work with GET requests
Altogether, you want a web.config that looks something like this:
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehavior>
<behavior name="restBehavior">
<webHttp />
</behavior>
</endpointBehavior>
<serviceBehavior>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehavior>
</behaviors>
<services>
<service name="..." behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="crossDomain"
contract="..." behaviorConfigurations="restBehavior" />
</service>
</services>
(Note that both the service and the endpoint have behaviours attached, allowing webHttp calls and httpGet calls respectively, and that the binding has crossDomain access explicitly enabled).
... a service method decorated like this:
[ServiceContract]
public interface IMyService
{
[WebGet] // Required Attribute to allow GET
[OperationContract]
string MyMethod(string MyParam);
}
... and a client call using JSONP:
<script type="text/javascript">
$(document).ready(function() {
var url = "...";
$.getJSON(url + "?callback=?", null, function(result) { // Note crucial ?callback=?
// Process result
});
});
</script>
这篇关于WCF错误:405不允许的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!