Custom Binding Part #2: creating a Basic REST service using CUSTOM Binding

In this article, I am going to show you how we could create a basic WCF REST service using custom binding.

Part 1 of this article series could be read Here

Follow the steps below,

Step 1

Create a new WCF service application project. Delete all the default codes getting created by WCF.

Step 2

Create the contract,

a

1. There is only one operation contract.

2. Function is returning Message and taking input parameter also as Message type.

3. * To ReplyAction says not to add reply action to the message.

4. * To Action will add that route all incoming request to this method.

Step 3

Implementing the service.

B

1. Using HTTPRequestMessageProperty class, I am finding absolute path for requesting URL.

2. Then I am checking whether request type is GET or not. If type is GET, I am displaying the message body.

3. Then I am checking the query string, if query string is there I am displaying that.

4. Then, I am creating the message. I am giving the message version as none. And appending a string. This will be displayed in browser as string.

5. Using HttpResponseMessageProperty class I am creating the response message. Then I am adding the custom header.

6. Then I am returning the response.

Compile the WCF Service application.

Step 4

Right click on the solution and add new project of the type console application. Right click on console application project and make it as startup project. Add the reference of System.ServiceModel and System.ServiceModel.Channel reference to the console project.

Step 5

Creating custom binding for REST Service.

C

 

 

1. I am creating object of CustomBinding class.

2. Then adding the binding elements in correct order.

3. For the transport adding the HttpTransportElement. Because we need to create a REST service and that follow only Http protocol.

4. Creating the service host and adding the endpoints.

Step 6

Go ahead and run the Console project. You will be getting the below console window.

D

Now open the browser and open the URL which you added as address in endpoint in the code. In my case it is Http://localhost:8889/TestHttp

E

And now when you see the command prompt you can see the other messages.

F

For your reference complete source code is as below ,

IService1.cs

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Runtime.Serialization;

    5 using System.ServiceModel;

    6 using System.ServiceModel.Web;

    7 using System.Text;

    8 using System.ServiceModel.Channels ;

    9 

   10 namespace MyService

   11 {  

   12     [ServiceContract]

   13     public interface IService1

   14     {

   15         [OperationContract(Action = “*”, ReplyAction = “*”)]

   16          Message  GetMessage(Message msg);

   17 

   18     }

   19 }

   20 

Service1.svc.cs

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Runtime.Serialization;

    5 using System.ServiceModel;

    6 using System.ServiceModel.Web;

    7 using System.Text;

    8 using System.ServiceModel.Channels ;

    9 

   10 namespace MyService

   11 {

   12     // NOTE: You can use the “Rename” command on the “Refactor” menu to change the class name “Service1” in code, svc and config file together.

   13     public class Service1 : IService1

   14     {

   15         public  Message   GetMessage(Message msg)

   16         {

   17             HttpRequestMessageProperty httpProps;

   18             string propName;

   19             propName = HttpRequestMessageProperty.Name;

   20             httpProps = msg.Properties[propName] as HttpRequestMessageProperty;

   21             string uri;

   22             uri = msg.Headers.To.AbsolutePath;

   23             Console.WriteLine(“Request to {0}”, uri);

   24             if (httpProps.Method != “GET”)

   25             {

   26                 Console.WriteLine(“Incoming Message {0} with method of {1}”,

   27                     msg.GetReaderAtBodyContents().ReadOuterXml(),

   28                     httpProps.Method);

   29 

   30             }

   31             else

   32             {

   33                 Console.WriteLine(“GET Request – no message body “);

   34             }

   35             if (httpProps.QueryString != null)

   36             {

   37                 Console.WriteLine(“QueryString = {0} “, httpProps.QueryString);

   38             }

   39             Message respone = Message.CreateMessage(MessageVersion.None, “*”, “Simple Response String”);

   40             HttpResponseMessageProperty responseProp = new HttpResponseMessageProperty();

   41             responseProp.Headers.Add(“CustomHeader”, “Value”);

   42             return respone;

   43 

   44         }

   45     }

   46 }

   47 

And in console application

Program.cs

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 using System.ServiceModel;

    6 using System.ServiceModel.Channels;

    7 using System.ServiceModel.Description;

    8 using MyService;

    9 

   10 

   11 namespace HostingApplication

   12 {

   13     class Program

   14     {

   15         static void Main(string[] args)

   16         {

   17             CustomBinding myBinding = new CustomBinding();

   18             TextMessageEncodingBindingElement msgEncoder = new TextMessageEncodingBindingElement();

   19             msgEncoder.MessageVersion = MessageVersion.None;

   20             myBinding.Elements.Add(msgEncoder);

   21             HttpTransportBindingElement bindingelemt = new HttpTransportBindingElement();

   22             myBinding.Elements.Add(bindingelemt);

   23             ServiceHost sh = new ServiceHost(typeof(Service1));

   24             ServiceEndpoint endpoint = null;

   25              endpoint = sh.AddServiceEndpoint(typeof(IService1),myBinding ,“Http://localhost:8889/TestHttp”);           

   26             sh.Open();

   27             Console.WriteLine(“REST Service is running”);

   28             Console.WriteLine(“Press a Key to close the service”);

   29             Console.ReadKey();

   30 

   31 

   32 

   33         }

   34     }

   35 }

   36 

 

I hope this was useful article. Thanks for reading. Happy coding

3 responses to “Custom Binding Part #2: creating a Basic REST service using CUSTOM Binding”

  1. Good article

  2. expunge pitiable
    erase sorry 4

  3. Very good resource. Add to bookmarks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com