How to define attributes to upload data to a REST web service implemented through a WCF service?

924 views Asked by At

I created a WCF service, that returns some data and also allow to post some data to. The methods of the service are as below:

    [OperationContract]
    bool UploadStream(Guid key, List<StreamRecord> values);

    [OperationContract]
    bool RegisterStream(Guid key);

    [OperationContract]
    StreamCollection GetStreams(Guid key);

I need to implement this with a REST interface. I created a new interface, IRestService as below

    [WebInvoke(
        Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "/bitpool/{poolKey}/streams")]
    BitStreamCollection GetBitStreams(string poolKey);

and it works ok (I can test it from the browser address bar and all is fine)

Now I want to implement also the Upload method, but I'm not sure how to do it I tried with

    [WebInvoke(
        Method = "POST",
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "/stream/{streamKey}/records/{values}")]
    bool UploadStream(string streamKey, List<StreamRecordEntity> values);

But when I try to access the service in browser it gives error

http://localhost:6767/RestServer.svc/

it gives an error:

Operation 'UploadBitStream' in contract 'IRestServerJson' has a path variable named 'values' which does not have type 'string'. Variables for UriTemplate path segments must have type 'string'.

I think for POST I cannot define such URL, but how should I do it?

Also, method RegisterStream should check if stream with key exists and if not, create it and return true, otherwise return false.

Should I define it as GET (since it must return result) or as PUT or POST?

Thanks

1

There are 1 answers

0
Jeff Ogata On BEST ANSWER

Pass the values in the body of the POST request, formatted in xml, not on the url. An easy way to test this is to use Fiddler.

Regarding RegisterStream, both POST and PUT can return information in the response body. You could use POST and return an appropriate HTTP status code depending on the action taken by the server: 201 Created if the resource is created, a different status code that makes sense in your application if the resource already exists.

The caller can determine whether the resource was created or already existed based on the HTTP status code returned, so the bool return value wouldn't be needed.

Side note: you can use the [WebGet(...)] instead of [WebInvoke(Method = "GET"...)].