views:

6150

answers:

6

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.

I've got my interface set up with the ServiceContract and 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/FileUpload but I get the "Method not Allowed" error

Am I missing something?

+1  A: 

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.

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].

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...)

A tremendous resource for WCF development is what we know in our shop as "the fish book": Programming WCF Services by 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.

Jeremy McGee
A: 

@jeremymcgee, I actually do have a DataContract setup:

[DataContract]
public class UploadedFile
{
    [DataMember]
    public string Name;

    [DataMember]
    public byte[] File;
}

Thanks for the book recommendation. I will take a look.

jdiaz
A: 

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
Ubiguchi
A: 

Check your web.config for verbs - Ensure GET and POST are allowed. Also check your IIS config for the same.

tsilb
can you be more specific?
Frank Schwieterman
+9  A: 

You are trying to invoke the method in a RESTful way. Make sure you have the WebGet attribute on the operation in the contract:

[ServiceContract]
public interface IService1
{
    [WebGet()]
    [OperationContract]
    string GetSomeData();
Ries
A: 

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.

darthjit