Ask a questionAsk a question
 

AnswerWcf to Rest

Answers

  • Tuesday, October 27, 2009 3:50 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello, you can refer to the %SDK Root%\Samples\ServiceBus\ExploringFeatures\Bindings\WebHttp\Image sample. It tells you how to perform GET operations with REST. POST and PUT is similar. For example, let's continue to build on the sample:

                [OperationContract(Action = "POST", ReplyAction = "POSTRESPONSE")]

                Message PostFile(Message file);

     

                public Message PostFile(Message file)

                {

                      XmlDictionaryReader reader = file.GetReaderAtBodyContents();

                      reader.MoveToContent();

                      reader.Read();

                      string encodedValue = reader.Value;

                      byte[] data = Convert.FromBase64String(encodedValue);

                      using (FileStream fs = File.Create("UploadedFile.txt"))

                      {

                            fs.Write(data, 0, data.Length);

                            fs.Flush();

                      } 

                      Message response = StreamMessageHelper.CreateMessage(OperationContext.Current.IncomingMessageVersion, "POSTRESPONSE", PostResponse);

                      HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();

                      responseProperty.Headers.Add("Content-Type", "text/plain");

                     response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);

                      return response;

                }

     

                private void PostResponse(Stream stream)

                {

                      string response = "File uploaded successfully!";

                      using (StreamWriter writer = new StreamWriter(stream))

                      {

                            writer.Write(response);

                            writer.Flush();

                      }

                }

     

    This service operation now supports POST. On the client side, you can work with the service as if it is a normal REST service:

                      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://solution.servicebus.windows.net/Image/");

                      request.ContentType = "text/plain";

                      using (Stream requestStream = request.GetRequestStream())

                      {

                            using (FileStream fs = File.Open("FileToUpload.txt", FileMode.Open))

                            {

                                  byte[] b = new byte[fs.Length];

                                  fs.Read(b, 0, b.Length);

                                  requestStream.Write(b, 0, b.Length);

                            }

                      }

                      HttpWebResponse response = (HttpWebResponse)request.GetResponse();

     

    Note in this sample, the service side has specified RelayClientAuthenticationType to None. That's why your client does not need to be authenticated. If the client requires authentication, you need to provide a X-MS-Identity-Token header in your request. You can refer to the HttpRouter sample on how to do that.


    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Tuesday, October 27, 2009 3:50 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hello, you can refer to the %SDK Root%\Samples\ServiceBus\ExploringFeatures\Bindings\WebHttp\Image sample. It tells you how to perform GET operations with REST. POST and PUT is similar. For example, let's continue to build on the sample:

                [OperationContract(Action = "POST", ReplyAction = "POSTRESPONSE")]

                Message PostFile(Message file);

     

                public Message PostFile(Message file)

                {

                      XmlDictionaryReader reader = file.GetReaderAtBodyContents();

                      reader.MoveToContent();

                      reader.Read();

                      string encodedValue = reader.Value;

                      byte[] data = Convert.FromBase64String(encodedValue);

                      using (FileStream fs = File.Create("UploadedFile.txt"))

                      {

                            fs.Write(data, 0, data.Length);

                            fs.Flush();

                      } 

                      Message response = StreamMessageHelper.CreateMessage(OperationContext.Current.IncomingMessageVersion, "POSTRESPONSE", PostResponse);

                      HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();

                      responseProperty.Headers.Add("Content-Type", "text/plain");

                     response.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);

                      return response;

                }

     

                private void PostResponse(Stream stream)

                {

                      string response = "File uploaded successfully!";

                      using (StreamWriter writer = new StreamWriter(stream))

                      {

                            writer.Write(response);

                            writer.Flush();

                      }

                }

     

    This service operation now supports POST. On the client side, you can work with the service as if it is a normal REST service:

                      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://solution.servicebus.windows.net/Image/");

                      request.ContentType = "text/plain";

                      using (Stream requestStream = request.GetRequestStream())

                      {

                            using (FileStream fs = File.Open("FileToUpload.txt", FileMode.Open))

                            {

                                  byte[] b = new byte[fs.Length];

                                  fs.Read(b, 0, b.Length);

                                  requestStream.Write(b, 0, b.Length);

                            }

                      }

                      HttpWebResponse response = (HttpWebResponse)request.GetResponse();

     

    Note in this sample, the service side has specified RelayClientAuthenticationType to None. That's why your client does not need to be authenticated. If the client requires authentication, you need to provide a X-MS-Identity-Token header in your request. You can refer to the HttpRouter sample on how to do that.


    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.