.net WCF 服务返回“不允许的方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF Service Returning "Method Not Allowed"
提问by jdiaz
In the process of developing my first WCF service and when I try to use it I get "Method not Allowed" with no other explanation.
在开发我的第一个 WCF 服务的过程中,当我尝试使用它时,我得到“Method not Allowed”,没有其他解释。
I've got my interface set up with the ServiceContract and OperationContract:
我已经使用 ServiceContract 和 OperationContract 设置了我的界面:
[OperationContract]
void FileUpload(UploadedFile file);
Along with the actual method:
连同实际方法:
public void FileUpload(UploadedFile file) {};
To access the Service I enter http://localhost/project/myService.svc/FileUploadbut I get the "Method not Allowed" error
要访问服务,我输入http://localhost/project/myService.svc/FileUpload但我收到“不允许的方法”错误
Am I missing something?
我错过了什么吗?
回答by darthjit
If you are using the [WebInvoke(Method="GET")]attribute on the service method, make sure that you spell the method name as "GET" and not "Get" or "get" since it is case sensitive! I had the same error and it took me an hour to figure that one out.
如果您[WebInvoke(Method="GET")]在服务方法上使用该属性,请确保将方法名称拼写为“GET”而不是“Get”或“get”,因为它区分大小写!我有同样的错误,我花了一个小时才弄明白。
回答by Ries
Your browser is sending an HTTP GET request: Make sure you have the WebGet attribute on the operation in the contract:
您的浏览器正在发送 HTTP GET 请求:确保您在合同中的操作上具有 WebGet 属性:
[ServiceContract]
public interface IUploadService
{
[WebGet()]
[OperationContract]
string TestGetMethod(); // This method takes no arguments, returns a string. Perfect for testing quickly with a browser.
[OperationContract]
void UploadFile(UploadedFile file); // This probably involves an HTTP POST request. Not so easy for a quick browser test.
}
回答by Jeremy McGee
The basic intrinsic types (e.g. byte, int, string, and arrays) will be serialized automatically by WCF. Custom classes, like your UploadedFile, won't be.
WCF 将自动序列化基本的内在类型(例如byte、int、string和数组)。自定义类,比如你的 UploadedFile,不会。
So, a silly question (but I have to ask it...): is UploadedFile marked as a [DataContract]? If not, you'll need to make sure that it is, and that each of the members in the class that you want to send are marked with [DataMember].
所以,一个愚蠢的问题(但我必须问它......):UploadedFile 是否标记为[DataContract]? 如果没有,您需要确保它是,并且您要发送的类中的每个成员都标有 [DataMember]。
Unlike remoting, where marking a class with [XmlSerializable] allowed you to serialize the whole class without bothering to mark the members that you wanted serialized, WCF needs you to mark up each member. (I believe this is changing in .NET 3.5 SP1...)
与远程处理不同,使用 [XmlSerializable] 标记一个类允许您序列化整个类,而无需费心标记您想要序列化的成员,WCF 需要您标记每个成员。(我相信这在 .NET 3.5 SP1 中正在改变......)
A tremendous resource for WCF development is what we know in our shop as "the fish book": Programming WCF Servicesby Juval Lowy. Unlike some of the other WCF books around, which are a bit dry and academic, this one takes a practical approach to building WCF services and is actually useful. Thoroughly recommended.
WCF 开发的一个巨大资源是我们在我们的商店中所知道的“鱼书”:Juval Lowy编写的 WCF 服务编程。与其他一些有点枯燥和学术性的 WCF 书籍不同,这本书采用了一种实用的方法来构建 WCF 服务,并且实际上很有用。彻底推荐。
回答by Ubiguchi
It sounds like you're using an incorrect address:
听起来您使用的地址不正确:
To access the Service I enter http://localhost/project/myService.svc/FileUpload
Assuming you mean this is the address you give your client code then I suspect it should actually be:
假设您的意思是这是您提供客户端代码的地址,那么我怀疑它实际上应该是:
http://localhost/project/myService.svc
回答by flobadob
I've been having this same problem for over a day now - finally figured it out. Thanks to @Sameh for the hint.
我已经有同样的问题超过一天了 - 终于想通了。感谢@Sameh 的提示。
Your service is probably working just fine. Testing POST messages using the address bar of a browser won't work. You need to use Fiddler to test a POST message.
您的服务可能运行良好。使用浏览器的地址栏测试 POST 消息将不起作用。您需要使用 Fiddler 来测试 POST 消息。
Fiddler instructions... http://www.ehow.com/how_8788176_do-post-using-fiddler.html
提琴手说明... http://www.ehow.com/how_8788176_do-post-using-fiddler.html
回答by dudeNumber4
My case: configuring the service on new server. ASP.NET 4.0 was not installed/registered properly; svc extension was not recognized.
我的情况:在新服务器上配置服务。ASP.NET 4.0 未正确安装/注册;无法识别 svc 扩展名。
回答by Sameh
Only methods with WebGet can be accessed from browser IE ; you can access other http verbs by just typing address
只能从浏览器 IE 访问带有 WebGet 的方法;您只需输入地址即可访问其他 http 动词
You can either try Restful service startup kit of codeples or use fiddler to test your other http verbs
您可以尝试使用 Restful service startup kit of codeples 或使用 fiddler 来测试您的其他 http 动词
回答by sandeep
you need to add in web.config
你需要在 web.config 中添加
<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="WcfRest.IService1"/>
<bindings>
<customBinding>
<binding name="basicConfig">
<binaryMessageEncoding/>
<httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
</binding>
</customBinding>

