Simple self-hosted web service in .NET

This tutorial demonstrates how to implement a simple self-hosted web service in a .NET console application based on Getting Started Sample.

Introduction

The service describes the operations it performs in a service contract that it exposes publicly as metadata. The service also contains the code to implement the operations and implements a contract that defines a request-reply communication pattern. The contract is defined by the ISelfHostedWebService interface, which exposes various operations (sum, mult, hello and helloworld). The client makes requests to a given operation and the service replies with the result. The service implements an ISelfHostedWebService contract that is defined in the following code.


Tutorial

  • First create a new Console Application in Visual Studio 2013
  • Add references for System.ServiceModel and System.ServiceModel.Web
  • Create a class that implements the ISelfHostedWebService
  • Start the console application
  • Use the Chrome plugin Restlet Client to test our self-hosted web service.

Send a POST request to: http://localhost:8135/webservice/helloworld

Add more demo REST calls (sum, mult, hello)

  • Test the sum web-service REST call
  • Test the mult web-service REST call
  • Test the hello web-service REST call

Source code

[sourcecode language=”csharp” wraplines=”false” collapse=”false”]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel.Web;
using System.Net;
using System.ServiceModel;

namespace SimpleSelfHostedWebService
{
[ServiceContract]
public interface ISelfHostedWebService
{
[OperationContract, WebInvoke(UriTemplate = “/sum”, Method = “POST”, BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
double sum(double x, double y);

[OperationContract, WebInvoke(UriTemplate = “/mult”, Method = “POST”,BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
double mult(double x, double y);

[OperationContract, WebInvoke(UriTemplate = “/hello”, Method = “POST”, BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string hello(string t);

[OperationContract, WebInvoke(UriTemplate = “/helloworld”, Method = “POST”, BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string helloworld();
}

public class SelfHostedWebService : ISelfHostedWebService
{
public double sum(double x, double y)
{
double result = (x + y);
Console.WriteLine(x + ” + ” + y + ” = ” + result);

return result;
}

public double mult(double x, double y)
{
double result = (x * y);
Console.WriteLine(x + ” * ” + y + ” = ” + result);

return result;
}

public string hello(string t)
{
string result = “hello: ” + t;
Console.WriteLine(result);

return result;
}

public string helloworld()
{
string result = “hello world”;
Console.WriteLine(result);

return result;
}
}

class Program
{
static void Main(string[] args)
{
string ip = “localhost”;
string port = “8135”;
string path = “/webservice”;

string baseAddress = “http://” + ip + “:” + port + path;
using (WebServiceHost myHost = new WebServiceHost(typeof(SelfHostedWebService), new Uri(baseAddress)))
{
Console.WriteLine(“starting service…”);
myHost.Open();

Console.WriteLine(“service started”);
Console.WriteLine(“press any key to stop the service”);

Console.Read();
myHost.Close();
}
}
}
}

[/sourcecode]


Read more


You may also be interested in