Objective
In this article, I will show you
- How to create a REST based service?
- How to host a REST based service in Console application?
- How to enable Help page for REST Service?
Follow the steps as below,
Step1
![image1.gif]()
Create a New project. Select Console application as project type.
Step 2
Add a new project to same solution. Choose the project type as class library.
![image2.gif]()
Step 3
Add below references in both projects console and service library
System.ServiceModel;
System.ServiceModel.Description;
System.ServiceModel.Web;
![image3.gif]()
If you are not able to get System.ServiceModel.Webdll by default , when you are
adding as Add Reference in your console application project and class library
project , then follow the below steps - Right click on your console application project or class library project
- Select properties from context menu
- 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.
![image4.gif]()
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- Open the Contract (Class Library project).
- Delete Class1.
- Right click and add a new item then select Interface from Code tab.
![image5.gif]()
- Make Interface as public and put ServiceContractattribute.
- Declare two operation contracts. Make one attributed with WebGet and another
attributed with Web Invoke.
IService.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
namespace Contracts
{
[ServiceContract]
publicinterfaceIService
{
[OperationContract]
[WebGet]
stringGetMessage(stringinputMessage);
[OperationContract]
[WebInvoke]
stringPostMessage(stringinputMessage);
}
}
Step 5
In this step, I will implement the contract in Service file. To do so,
- Right click and add a class in Console application.
![image6.gif]()
- Give any name; I am giving name here Service of the class.
- Implement the interface (Contract IService) in this class.
Service.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using Contracts;
namespaceSelfHostedRESTService
{
publicclassService
:IService
{
publicstringGetMessage(stringinputMessage)
{
return"Calling Get for you " + inputMessage;
}
publicstringPostMessage(stringinputMessage)
{
return"Calling Post for you " + inputMessage;
}
}
}
Step 6
In this step, I will host the service in a console application. So to do so
- Open Program.cs
- Create instanced ofWebServieceHostFactory
![image7.gif]()
- Add a service end point
![image8.gif]()
- Add service host behavior with help page enabled
![image9.gif]()
As we know, REST service used webHttpBindding. And we are enabling Help page
here.
Program.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Description;
usingSystem.ServiceModel.Web;
using Contracts;
namespaceSelfHostedRESTService
{
classProgram
{
staticvoid Main(string[] args)
{
WebServiceHost host = newWebServiceHost(typeof(Service),
newUri("http://localhost:8000"));
ServiceEndpointep = host.AddServiceEndpoint(typeof(IService),
newWebHttpBinding(),
"");
host.Description.Endpoints[0].Behaviors.Add(newWebHttpBehavior
{ 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.
![image10.gif]()
Now once service is up and running, I can test,
To test the Help page, just type
http://localhost:8000/help
![image11.gif]()
When you click on Get or Post link you will get below detail help.
![image12.gif]()
I hope this post was useful. Thanks for reading. Happy coding .