SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Igor Merabishvili Articles | WCF with C# January 25, 2011
In this article you will learn how to use Simple WCF web service to receive parameter from HTTP POST request body.
Reader Level:

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

 <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="EchoService.IService1">

- add the following code under behaviors section:

<endpointBehaviors>
  <behavior name="webBehavior">
    <webHttp/>
  </behavior>
</endpointBehaviors>

4. Open IService1.cs file and replace it's content with the following code:

using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.IO;

namespace EchoService
{
    [ServiceContract]
    public interface IService1
    {
        [WebInvoke(UriTemplate = "echo")]
        Stream HandleMessage(Stream request);
    }
}

5. Open Service1.cs file and replace it's content with the following code:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.IO;
using System.ServiceModel.Web;

namespace EchoService
{
    public class Service1 : IService1
    {
        public Stream HandleMessage(Stream request)
        {
            StreamReader reader = new StreamReader(request);
            string text = "EchoServer received: " + reader.ReadToEnd();
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            MemoryStream ms = new MemoryStream(encoding.GetBytes(text));
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            return ms;
        }
    }
}

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.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor