views:

70

answers:

4

Hello, I'm trying to dive into the RESTful web services world and have started with the following template:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Test {
   // TODO: Implement the collection resource that will contain the SampleItem instances

   [WebGet(UriTemplate = ""), OperationContract]
   public List<SampleItem> GetCollection() {
     // TODO: Replace the current implementation to return a collection of SampleItem instances
     return new List<SampleItem>() {new SampleItem() {Id = 1, StringValue = "Hello"}};
   }

   [WebInvoke(UriTemplate = "", Method = "POST"), OperationContract]
   public SampleItem Create(SampleItem instance) {
     // TODO: Add the new instance of SampleItem to the collection
      throw new NotImplementedException();
   }

   [WebGet(UriTemplate = "{id}"), OperationContract]
   public SampleItem Get(string id) {
      // TODO: Return the instance of SampleItem with the given id
      throw new NotImplementedException();
   }

   [WebInvoke(UriTemplate = "{id}", Method = "PUT"), OperationContract]
   public SampleItem Update(string id, SampleItem instance) {
      return new SampleItem {
               Id = 99,
               StringValue = "Done"
             };
   }

   [WebInvoke(UriTemplate = "{id}", Method = "DELETE"), OperationContract]
   public void Delete(string id) {
      // TODO: Remove the instance of SampleItem with the given id from the collection
      throw new NotImplementedException();
   }
}

I am able to perform the GET operation but I am unable to perform PUT, POST or DELETE requests.

Can anyone explain me how to perform these operations and how to create the correct URLs?

Best regards

Alessandro

A: 

As far as I know, WebInvoke only supports GET and POST at this time. I use POST to perform PUT and DELETE action instead.

ZEAXIF
Could you please write me an example on how to write the request for this method?[WebInvoke(UriTemplate = "", Method = "POST"), OperationContract] public SampleItem Create(SampleItem instance)....I cannot write a request for it and get the response.Best regardsAlessandro
Alex
@Alex, did you even read what Zeaxif said?
Polaris878
Patrick
+1 @Patrick, if PUT and DELETE are all valid, I think the next thing to do is to check configuration of web service.
ZEAXIF
Either way, you have to break your object (SampleItem) into individual parameters in the OperationContract. See my example.
Patrick
A: 

Could you please write me an example on how to write the request for this method? [WebInvoke(UriTemplate = "", Method = "POST"), OperationContract] public SampleItem Create(SampleItem instance)....

I cannot write a request for it and get the response.

Best regards

Alessandro

Alex
A: 

EDIT - Updated in response to your answer:

URL is "http://localhost/test/Test.svc/MethodName"
postData is the data you want to pass as a parameter.

In your case, it looks like you're trying to pass a type. Remember this is being posted in a URL. Break the values of the type into parameters.

Example: "http://localhost/test/Test.svc/Create?id=123456&amp;stringValue=newSampleItem"

You'll need to change the Operation Contract to accept an int and a string instead of a SampleItem.

    [WebInvoke(UriTemplate = "Create?id={x}&stringValue={y}", Method = "POST"), OperationContract]
    public SampleItem Create(int id, string stringValue)
    {
       // Create and return the Sample Item. 
    }

Let me know how it goes.

Patrick.

Hi Alex, This is what I use to post to a Restful service...

// Create the request
WebRequest request; 
request = WebRequest.Create(url + postData); 
request.Method = "POST";  

byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
request.ContentType = "application/x-www-form-urlencoded"; 
request.ContentLength = byteArray.Length;

// Get the request stream. 
Stream dataStream = request.GetRequestStream(); 

// Write the data to the request stream. 
dataStream.Write(byteArray, 0, byteArray.Length); 

// Close the Stream object. 
dataStream.Close();

// Process the response
Stream responseStream; 
responseStream = request.GetResponse().GetResponseStream();

StreamReader objReader = new StreamReader(responseStream); 

StringBuilder sb = new StringBuilder(); 
string sLine = ""; 
int i = 0; 

while (sLine != null) 
{
    i++;
    sLine = objReader.ReadLine();
    sb.Append(sLine);
}

responseStream.Close();


string responseXML = sb.ToString()

Good Luck,

Patrick

Patrick
A: 

Hello Patrick, what do you mean with "url + postData". Suppose that my service url is http://localhost/test/Test.svc/

How is my post data formed supposing that my SampleItem is made like that:

public class SampleItem {
        public int Id { get; set; }
        public string StringValue { get; set; }
}

Sorry for the stupid questions.

Alessandro

Alex
See my original answer, I've edited it with a response to your question.
Patrick