How to Self Host ASP.NET Web Api

OWINOver the past several months, I’ve talked to a few people that were completely unaware that you could self host ASP.NET Web Api application without the need for IIS.

Anyone who has worked with IIS knows that sometimes it can feel a bit heavy.  Especially if you not using any other features other than serving static content or executing your ASP.NET Web Api.

There are a couple important aspects to cover first which are what makes self hosting possible.

If you have any questions, please follow me on Twitter.

OWIN

OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.

To clarify and reword slightly, OWIN is a open source specification that is not defined by microsoft and is not an actual implementation.

Katana

Katana is an open source project by the Microsoft Open Technologies.  It’s a set of components for building and hosting OWIN-based web applications that follows the OWIN  specification.

I’m not entirely sure what the status is of OWIN/Katana within ASP.NET 5 (vNext), however I would like to think the separation between server, host, middleware, and application will continue.

Video Tutorial

If you prefer to follow along with a video tutorial, take a look at the video below.

Demo

All of the source code for this demo is available on Github.

The first thing we are going to do for this demo is create a Console Application and add the Owin SelfHost and WebAPI Owin Selfhost nuget packages

PM> Install-Package Microsoft.Owin.SelfHost

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

Next, we will create a Startup class that will contain the HttpConfiguration similar to how you configure Web Api currently.   The startup class is the implementation of the OWIN specification that our OWIN Self Host implementation will use.  The Startup class must provide a public Configuration() method with the signature void.

Next, we will create a simple ApiController.

Last step is to add the web server implementation to out Program.cs Main() method.  This is done by simply calling out Katana implementation of WebApp.Start and passing our Startup class name as a type argument along with the host/port binding details as the first parameter argument.

That’s it!  Now when I make a call from Postman to http://localhost:8080/api/demo, here are the results:

selfhost

 

2-hour Video Course for $30 USD

Sign up for more info!

* indicates required




 

Source Code

GithubThe source code for this demo is available on Github.

More

Event Stream as a Message Queue

I was recently having a discussion around a system being built using Microsoft Azure.  Some concepts being discussed for this system where CQRS, Event Sourcing and Message Queue.

The diagram below is fairly typical when discussing CQRS and Event Sourcing.

Message Queue

One of the first things that stood out to me was the use of the Message Queue and Azure Service Bus.

For this blog post, I want to focus on the Service bus, which is used for publish-subscribe pattern.  The domain will emit events that are stored to the event stream and then will be published to the Service Bus.  Subscribers, such as Projections or other Bounded Contexts will process these events.

Forgotten Option

There is nothing wrong with using a service bus to publish domain events.

However, one option which is seemingly always forgotten to developers that are new to CQRS and Event Sourcing:

Your event stream can be used as a message queue

Other bounded contexts or projections can query/poll your event storage to retrieve new events that have been persisted.

At regular intervals, the event consumer could poll your event storage requesting any number of new events based on the last event it processed.

The consumer would be required to keep track of the last event it processed in order.  This provides some benefits as it may not be a process that is required to be continuously running.

You may be thinking: Polling? Really?

If you rolled your own event storage, I could understand how this might be problematic and would likely be easier to use a service bus.  Or you may want your event consumers to process the event as soon as possible.  Your implementation of how to handle this is dependant on how you are currently storing your events.

But the point still remains: Your event stream is a message queue.

As always, context is king.  Requirements and many other factors will play into how you want to handle messaging.

It is another option that may fit a scenario that you run into.

Event Store

Event Store

If you are just thinking about getting into Event Sourcing, I would highly recommend looking at Event Store by Greg Young as your event storage.

Event Store supports multiple types of Subscriptions including Persistent Subscriptions for the Competing Consumers messaging pattern.

ConfigR: Look Mom, No XML

driving-nohandsI stumbled upon ConfigR a several months ago  on twitter from Glenn Block.  It uses ScriptCS (and Roslyn) and was developed by Adam Ralph.

I finally got around to take it for a test drive on a side project.  It’s really a simple tool that I’ve been waiting for.

Why ConfigR?

The purpose of ConfigR is pretty straight forward.  It allows you to write C# code to define configuration settings which you would normally place in a the appSettings section of a web.config/app.config.

If you have used the appSettings at all, you probably wished they could be typed instead of always having to be strings.

From Adam Ralph, the creator of ConfigR

ConfigR allows you to write your config in C# instead of XML – that’s really all there is to it. From that point on you can use your imagination to go wild in what you do in that C#.

Install via NuGet

Make sure your project is set to be using .NET 4.5 or greater

You need to get ConfigR into your project.  Install via the Package Manager in Visual Studio.

PM> Install-Package ConfigR

Or if you prefer via the NuGet UI

configr2

Web Application

This portion has been updated to reflect the proper way of sing ConfigR with a web application.  Thanks to creator Adam Ralph for clarification below in the comments.

The default naming convention ConfigR uses to automatically load your configuration file is to look for a file in your output directly named Web.csx.

  1. Create a new file in your project named Web.csx
  2. Make sure the Copy to Output Directory is set to Do not copy.

This is different than a console app in which you do want to copy to output dir.  In a web application, we want the Web.csx in the same location as you would normally see the web.config.

ConfigR

I’m going to create two simple configuration settings in our new Web.csx file

As you can expect the Add method is adding items into the ConfigR global configuraiton.  The two configuration settings I’ve created are of different types: boolean and string.

Now in code when we need to access these configuration settings, it couldn’t be any easier.

Loading from Custom Location

Want to load a config file not using the default naming convention? Easy.

Complex Types

You can use complex types defined in your assembly or another referenced assembly.  When using a referenced assembly, you must specify the ScriptCS reference directive to the DLL in bin/ directory.

Here is my type defined in another referenced project called AnotherProject.dll

Here is my new Web.csx.  Again, note the #r ScriptCS reference directive and the using statement.

Now we can get our our complex type.