Simple WCF web service to receive parameter from HTTP POST request body

In this article, you will learn how to use Simple WCF web service to receive parameter from HTTP POST request body.

One week ago I started a new project to replace an old web service with the new one. The idea was to replace the old unmaintainable code with a brand new solution based on WCF. As we had to keep the old clients running we needed to keep our new service able to response to HTTP POST requests. The request parameter had to be delivered in the body of the request. So I tried to find a simple example how to build in Visual Studio 2010 WCF web service to receive parameter from HTTP POST request body. I found a lot of useful hints but not the complete sample. As I finally managed to compile my own solution I'd like to share it. 
 
1. Create a new project in Visual Studio based on "WCF Service Library" template.
 
01_NewProject.jpg 
 
2. Go to the project properties and change target framework to ".NET Framework 4".
 
02_Framework.jpg 
 
3. Open App.config file and do the following changes:
  • change base address to http://localhost:8000/
  • replace endpoint section to
    1. <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="EchoService.IService1">
  • add the following code under behaviors section:
    1. <endpointBehaviors>    
    2.   <behavior name="webBehavior">    
    3.     <webHttp/>    
    4.   </behavior>    
    5. </endpointBehaviors>
4. Open IService1.cs file and replace it's content with the following code:
  1. using System;  
  2. using System.ServiceModel;  
  3. using System.ServiceModel.Web;  
  4. using System.IO;  
  5. namespace EchoService  
  6. {  
  7.     [ServiceContract]  
  8.     public interface IService1  
  9.     {  
  10.         [WebInvoke(UriTemplate = "echo")]  
  11.         Stream HandleMessage(Stream request);  
  12.     }  
  13. }  
5. Open Service1.cs file and replace it's content with the following code:
  1. using System;  
  2. using System.ServiceModel;  
  3. using System.ServiceModel.Channels;  
  4. using System.IO;  
  5. using System.ServiceModel.Web;  
  6. namespace EchoService  
  7. {  
  8.     public class Service1 : IService1  
  9.     {  
  10.         public Stream HandleMessage(Stream request)  
  11.         {  
  12.             StreamReader reader = new StreamReader(request);  
  13.             string text = "EchoServer received: " + reader.ReadToEnd();  
  14.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();  
  15.             MemoryStream ms = new MemoryStream(encoding.GetBytes(text));  
  16.             WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";  
  17.             return ms;  
  18.         }  
  19.     }  
  20. }  
6. Start debugging. If everything OK then Visual Studio starts WCF Test Client. Now you can open you favorite test tool (I use Fiddler) and send a post request to
 
http://localhost:8000/echo
 
7. In my case I sent a word "test" as request and received back "EchoServer received: test".
 
Enjoy.