Self hosting Web API service (RabbitMQ callable)

Sunny Ahuwanya edited this page Jan 25, 2016 · 12 revisions

Follow these steps to call a self hosted Web API service through RabbitMQ using the RestBus library.

Create a new Console application, which will serve as the service and install the Microsoft.AspNet.WebApi.OwinSelfHost, RestBus.RabbitMQ and RestBus.WebApi NuGet packages.

PM> Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
PM> Install-Package RestBus.RabbitMQ
PM> Install-Package RestBus.WebApi

Create the Web API self hosting service and add RestBus hosting code

This code example is a modification of the Use OWIN to Self-Host ASP.NET Web API tutorial

  • You need an installation of RabbitMQ to run the code sample.
  • Note that samba is the unique name of the service in this example.
using Microsoft.Owin.Hosting;
using Owin;
using RestBus.RabbitMQ;
using RestBus.RabbitMQ.Subscription;
using RestBus.WebApi;
using System;
using System.Collections.Generic;
using System.Web.Http;

class Program
{
    static void Main()
    {
        //Initialize startup object
        var startup = new Startup();

        string baseAddress = null;
        //baseAddress = "http://localhost:9000/"; //Uncomment this line to also listen on localhost:9000

        //Start WebAPI OWIN host 
        using (WebApp.Start(url: baseAddress, startup: startup.Configuration))
        {
            //Start RestBus Subscriber/host

            var amqpUrl = "amqp://localhost:5672"; //AMQP URI for RabbitMQ server
            var serviceName = "samba"; //Uniquely identifies this service

            var msgMapper = new BasicMessageMapper(amqpUrl, serviceName);
            var subscriber = new RestBusSubscriber(msgMapper);
            using (var host = new RestBusHost(subscriber, startup.Config))
            {
                host.Start();
                Console.ReadLine();
            }
        }
    }
}

public class Startup
{
    HttpConfiguration config = new HttpConfiguration();

    public HttpConfiguration Config
    {
        get { return config; }
    }

    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        appBuilder.UseWebApi(config);
    }
}

public class ValuesController : ApiController
{
    // GET api/values 
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5 
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values 
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5 
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5 
    public void Delete(int id)
    {
    }
}

A working example of a self hosted Web API service is in the Examples project.

Make a request to the service

See Calling a Service EndPoint

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.