Objective

In this article, I will show you

1. How to create a REST based service?

2. How to host a REST based service in Console application?

3. How to enable Help page for REST Service?

Follow the steps as below,

Step1

Create a New project. Select Console application as project type.

clip_image002

Step 2

Add a new project to same solution. Choose the project type as class library.

clip_image004

Step 3

Add below references in both projects console and service library

System.ServiceModel;

System.ServiceModel.Description;

System.ServiceModel.Web;

clip_image005

If you are not able to get System.ServiceModel.Web dll by default , when you are adding as Add Reference in your console application project and class library project , then follow the below steps

1. Right click on your console application project or class library project

2. Select properties from context menu

3. From Application tab, if you see by default .NET Framework 4 Client profile is selected. Change that to .NET Framework 4.0. So your target framework should be .NET Framework 4.0.

clip_image006

Now you should able to see all the dll when you add service reference in project.

Step 4

In this step, I will create contract of service

1. Open the Contract (Class Library project).

2. Delete Class1.

3. Right click and add a new item then select Interface from Code tab.

clip_image008

4. Make Interface as public and put ServiceContract attribute.

5. Declare two operation contracts. Make one attributed with WebGet and another attributed with Web Invoke.

IService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Web;

 

namespace Contracts

{

    [ServiceContract]

    public  interface IService

    {

        [OperationContract]

        [WebGet]

        string GetMessage(string inputMessage);

 

        [OperationContract]

        [WebInvoke]

        string PostMessage(string inputMessage);

    }

}

 

Step 5

In this step, I will implement the contract in Service file. To do so,

1. Right click and add a class in Console application.

clip_image010

2. Give any name; I am giving name here Service of the class.

3. Implement the interface (Contract IService) in this class.

Service.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Contracts;

 

namespace SelfHostedRESTService

{

   public  class Service :IService

    {      

 

 

     public string GetMessage(string inputMessage)

        {

            return "Calling Get for you " + inputMessage;

        }

 

     public string PostMessage(string inputMessage)

     {

         return "Calling Post for you " + inputMessage;

     }

    }

}

 

Step 6

In this step, I will host the service in a console application. So to do so

1. Open Program.cs

2. Create instanced of WebServieceHostFactory

clip_image012

3. Add a service end point

clip_image014

4. Add service host behavior with help page enabled

clip_image016

As we know, REST service used webHttpBindding. And we are enabling Help page here.

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Description;

using System.ServiceModel.Web;

using Contracts;

 

namespace SelfHostedRESTService

{

    class Program

    {

        static void Main(string[] args)

        {

 

 

 

            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000"));

            ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");

 

            host.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });          

            host.Open();

            Console.WriteLine("Service is up and running");

            Console.WriteLine("Press enter to quit ");

            Console.ReadLine();

            host.Close();

 

 

 

        }

    }

}

 

Step 7

Just press F5 to run the service.

clip_image018

Now once service is up and running, I can test,

To test the Help page, just type

http://localhost:8000/help

clip_image020

When you click on Get or Post link you will get below detail help.

clip_image022

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

Advertisements

One thought on “Enabling Help page for REST Service in WCF 4.0

  1. As you noted here you cannot use the client profile to accomplish this. do you know a technique that is availible in the client profile to accomplish the same thing?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s