Getting started with RESTful web services on WCF and Nelibur

Original author: Sergey Morenko
  • Transfer
  • Tutorial
WCF is a really powerful thing, but over and over for new projects, we are forced to create new, similar web services. In this article (translation) we will see how to create a reusable RESTful message based web service using Nelibur . Nelibur is a Message based web service framework on pure WCF. Let's see how to start creating web services on WCF using Nelibur.

Step 1. Create an application


For simplicity, the WCF service will host the console application. So just add a new console project.


Step 2. Install Nelibur


The easiest way to install Nelibur is to use the NuGet package manager .


You can do the same with the Package Manager Console.


Now we are ready to create a RESTful WCF message based service.

Step 3. Create a WCF service


For example, we take the following requirements for the WCF service:

Nelibur already contains the implementation we need: JsonServicePerCall .
So, we add the launch code of our service.
internal class Program
{
    private static WebServiceHost _service;
    private static void Main()
    {
        _service = new WebServiceHost(typeof(JsonServicePerCall));
        _service.Open();
        Console.WriteLine("ShipTrackingService is running");
        Console.WriteLine("Press any key to exit\n");
        Console.ReadKey();
        _service.Close();
    }
} 

We prescribe the configuration


We implement business logic


Our service should be able to:
  • Add ship
  • Receive ship by ShipId

We create a team AddShipCommand
public sealed class AddShipCommand
{
    public string ShipName { get; set; }
} 

The result of the command should be an object ShipInfo
public sealed class ShipInfo
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

Add a query ShipLocationQuery, the result of which should be an objectShipLocation
public sealed class ShipLocationQuery
{
    public Guid ShipId { get; set; }
}
public sealed class ShipLocation
{
    public string Location { get; set; }
    public Guid ShipId { get; set; }
}

Now we need to make a command and request handler
public sealed class ShipProcessor : IPost,
                                    IGet
{
    private static readonly Dictionary _ships = new Dictionary();
    public object Get(ShipLocationQuery request)
    {
        if (_ships.ContainsKey(request.ShipId))
        {
            return new ShipLocation
            {
                Location = "Sheldonopolis",
                ShipId = request.ShipId
            };
        }
        throw new WebFaultException(HttpStatusCode.BadRequest);
    }
    public object Post(AddShipCommand request)
    {
        var ship = new Ship(request.ShipName, Guid.NewGuid());
        _ships[ship.Id] = ship;
        return new ShipInfo
        {
            Id = ship.Id,
            Name = ship.Name
        };
    }
} 

Bind the command and request to the handler
internal class Program
{
    private static WebServiceHost _service;
    private static void ConfigureService()
    {
        NeliburRestService.Configure(x =>
        {
            x.Bind();
            x.Bind();
        });
    }
    private static void Main()
    {
        ConfigureService();
        _service = new WebServiceHost(typeof(JsonServicePerCall));
        _service.Open();
        Console.WriteLine("ShipTrackingService is running");
        Console.WriteLine("Press any key to exit\n");
        Console.ReadKey();
        _service.Close();
    }
}

Everything, the service is over. As you noticed, you can add any operations without changing the WCF service ...

Client using our WCF service


As a client, you can use:

Fiddler


Adding a new ship (using a POST request):


Receiving a ship using ShipId (using a GET request):


JsonServiceClient


To create your client - add another console application.
Here is the client code:
internal class Program
{
    private static void Main()
    {
        var client = new JsonServiceClient(Settings.Default.ServiceAddress);
        var shipInfo = client.Post(new AddShipCommand { ShipName = "Star" });
        Console.WriteLine("The ship has added: {0}", shipInfo);
        var shipLocation = client.Get(new ShipLocationQuery { ShipId = shipInfo.Id });
        Console.WriteLine("The ship {0}", shipLocation);
        Console.ReadKey();
    }
} 

We launch our client and see the result of the execution:

On the service


side On the client side


additional literature




That's all


I hope you enjoyed it. Thanks for reading the article (translation).
Sources can be downloaded from the original .

From translator


When reading the article, I recommend paying attention to the code of a bunch of requests and handlers:
    private static void ConfigureService()
    {
        NeliburRestService.Configure(x =>
        {
            x.Bind();
            x.Bind();
        });
    }

Adding new functionality to the service will be reduced to creating a request / command class, a handler class, and their bundle. Easily and effortlessly organized and used cute heart CQRS. Nelibur allows you to organize this miracle with minimal labor. And if you are a little savvy, then a bunch of requests \ commands and handlers can be done automatically and then the magic will become complete :)

Also popular now: