Invoke REST service through C# Console

The below piece of code will help to implement REST service on the fly with out having them to deploy at IIS or any supportive platforms.

This will help you to quickly test how your service behaves on the fly. 

namespace Adapter
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Adapter initializing ...");
string tmp = SPOCredentials.Current.UserName;
Console.WriteLine("");
Console.WriteLine("Starting Service ...");

WebServiceHost host = new WebServiceHost(typeof(SPOAdapterService), new Uri("http://localhost:1234/"));
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IAdapterService), new WebHttpBinding(), "");
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.HttpHelpPageEnabled = false;

host.Open();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("");
Console.WriteLine("Service is running until you press enter to quit!");
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Sample request 1: http://localhost:1234/scan?t=FETCH&m=10&r=10");
Console.WriteLine("Sample request 2: http://localhost:1234/scan?t=PULL&m=100&u=07.03.2016 09:30:00");
Console.WriteLine("");
Console.WriteLine();

Console.ReadLine();
host.Close();
}
}
}

Methods can look like below

namespace Adapter.Service
{

[ServiceContract]
public interface AdapterService
{
[OperationContract]
[WebGet (BodyStyle = WebMessageBodyStyle.Bare)]
XElement scan(string t, string m, string r, string u, string uc);

[OperationContract]
[WebGet (BodyStyle = WebMessageBodyStyle.Bare)]
Stream get(string id, string indexer)
class SPOAdapterService : IAdapterService
{
public Stream get(string id, string indexer)
{
throw new NotImplementedException();
}

public XElement scan(string t, string m, string r, string u, string uc)
{
SPOCrawlRuntime runtime = new SPOCrawlRuntime();
return result
}

Press F5 and Service will start running

While the service is running, you can directly invoke the methods by opening IE and constructing the url

You can debug the methods by setting up the breakpoints

Tags :

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.