Skip to content

2. Hosting

Jesse Sweetland edited this page Jun 14, 2017 · 11 revisions

Loopback

The Platibus.LoopbackHost class in the main Platibus package initializes a bus in which all sent messages, publications, and replies are routed back to itself. This is useful for implementing local event buses or for integration testing. To start the host, simply call the static LoopbackHost.Start method:

var host = await LoopbackHost.Start()

The following is a basic loopback host executable that will run until halted or a key is pressed:

public class Program
{
    public static void Main(string[] args)
    {
        var host = Task.Run(async () => await LoopbackHost.Start()).Result;
        Console.WriteLine("Press any key to stop the server...");
        Console.ReadKey();
        host.Dispose();
    }
}

The LoopbackHost.Start method has two overloads. The first accepts an optional string parameter specifying the name of the Platibus.Configuration.LoopbackConfigurationSection to load from the app.config (the default section name is "platibus.loopback"). If this overload is used, the loopback host will be configured according to the declarative configuration specified in the named configuration section, filtered through any concrete public IConfigurationHook implementations found in any of the assemblies in the the app domain base directory.

For more information on Loopback server configuration, refer to the Loopback server configuration page.

HTTP Server

The Platibus.Http.HttpServer class in the main Platibus package is a basic HTTP server that hosts a bus and transmits messages via HTTP post requests. To start the server, simply call the static HttpServer.Start method:

var server = await HttpServer.Start()

The following is a basic HTTP server executable that will run until halted or a key is pressed:

public class Program
{
    public static void Main(string[] args)
    {
        var httpServer = Task.Run(async () => await HttpServer.Start()).Result;
        Console.WriteLine("Press any key to stop the server...");
        Console.ReadKey();
        httpServer.Dispose();
    }
}

The HTTP server can also be hosted in a Windows service:

public class HttpService : ServiceBase
{
    protected HttpServer HttpServer;

    public HttpService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        HttpServer = Task.Run(async () => await HttpServer.Start()).Result;
    }

    protected override void OnStop()
    {
        if (HttpServer != null)
        {
            HttpServer.Dispose();
            HttpServer = null;
        }
    }
}

The HttpServer.Start method has two overloads. The first accepts an optional string parameter specifying the name of the Platibus.Http.HttpServerConfigurationSection to load from the app.config (the default section name is "platibus.http"). If this overload is used, the HTTP server will be configured according to the declarative configuration specified in the named configuration section, filtered through any concrete public IConfigurationHook implementations found in any of the assemblies in the the app domain base directory.

For more information on HTTP server configuration, refer to the HTTP server configuration page.

IIS

A Platibus instance can be hosted in IIS using the Platibus.IIS.PlatibusHttpHandler supplied in the Platibus.IIS package. To register the handler, add the following to your web.config:

<system.webServer>
    <handlers>
        <add name="platibus" verb="*" path="platibus" type="Platibus.IIS.PlatibusHttpHandler, Platibus.IIS" />
    </handlers>
</system.webServer>

The path can be anything you like, but it must agree with the baseUri specified in the <platibus> configuration section. In the above example, the configured baseUri would be http://<host>:<port>/platibus where host and port are the hostname and port specified in the http protocol binding in the web site configuration.

The bus instance will be configured in the HTTP context and can be retrieved from within a WebForm or controller via the GetBus extension method of the HttpContextExtensions class:

public ActionResult Action(Model model)
{
    var bus = HttpContext.GetBus();
}

Due to the inability to specify extra configuration parameters in the handler element, only a single Platibus instance can be hosted. Further, the Platibus configuration must use the default section name platibus in order to be recognized and honored by the HTTP handler.

For more information on IIS hosting configuration, refer to the IIS configuration page.

For IIS hosting examples, see The Platibus.SampleWebApp project provided in the GitHub repository (https://github.com/sweetlandj/Platibus/tree/master/Source/Platibus.SampleWebApp). This is a very basic application that demonstrates how to configure and use Platibus in an ASP.Net MVC 5 application (.NET 4.5.1 runtime required).

OWIN Middleware

Platibus can also be configured to serve HTTP requests via OWIN middleware. The Platibus.Owin package features a middleware implementation Platibus.Owin.PlatibusMiddleware that can will route all requests beginning with the configured baseUri. To configure the middleware, add the following to the Configuration method of the OWIN startup class:

app.UsePlatibusMiddleware();

The UsePlatibusMiddleware method has overrides to specify different configuration section names (the default is platibus.owin); a custom IOwinConfiguration object; or a programmatically configured PlatibusMiddleware instance. The bus instance will be configured in the OWIN context and can be retrieved from within a controller via the GetBus extension method of the OwinContextExtensions class:

public IHttpActionResult Post(Model m)
{
    var bus = Request.GetOwinContext().GetBus();
}

For OWIN hosting examples, see The Platibus.SampleApi project provided in the GitHub repository (https://github.com/sweetlandj/Platibus/tree/master/Source/Platibus.SampleApi). This is a very basic application that demonstrates how to configure and use Platibus in an ASP.Net MVC 5 / WebAPI application (.NET 4.5.2 runtime required).

Rabbit MQ

Platibus supports the use of RabbitMQ queues for transport and queueing messages. Like the HTTP server, a host class is provided that implementers can call from their own executables or Windows services.

The Platibus.RabbitMQ.RabbitMQHost class in the Platibus.RabbitMQ package starts the bus instance and initializes the RabbitMQ queues and exchanges necessary to send, subscribe, and queue messages. To start the host, simply call the static RabbitMQHost.Start method:

var server = await RabbitMQHost.Start()

The following is a basic RabbitMQ host executable that will run until halted or a key is pressed:

public class Program
{
    public static void Main(string[] args)
    {
        var rabbitMQHost = Task.Run(async () => await RabbitMQHost.Start()).Result;
        Console.WriteLine("Press any key to stop the server...");
        Console.ReadKey();
        rabbitMQHost.Dispose();
    }
}

The RabbitMQ host can also be hosted in a Windows service:

public class RabbitMQHostingService : ServiceBase
{
    protected RabbitMQHost RabbitMQHost;

    public RabbitMQHostingService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        RabbitMQHost = Task.Run(async () => await RabbitMQHost.Start()).Result;
    }

    protected override void OnStop()
    {
        if (RabbitMQHost != null)
        {
            RabbitMQHost.Dispose();
            RabbitMQHost = null;
        }
    }
}

The RabbitMQHost.Start method has two overloads. The first accepts an optional string parameter specifying the name of the Platibus.RabbitMQ.RabbitMQHostConfigurationSection to load from the app.config (the default section name is "platibus.rabbitmq"). If this overload is used, the HTTP server will be configured according to the declarative configuration specified in the named configuration section, filtered through any concrete public IConfigurationHook implementations found in any of the assemblies in the the app domain base directory.

For more information on HTTP server configuration, refer to the RabbitMQ configuration page.