This is the documentation for the nuget package Unity.SelfHostWebApiOwin.
The package is dependent on Katana 2.
The code can be found here:
Example, how to use: https://github.com/damienbod/HelloWorldUnityWithSelfHostedOwin
Source code: https://github.com/damienbod/SelfHostWebApiWithOwinAndUnityUsingNuget
How to create an application using the nuget package:
Step 1: Create a Console application. This will be used to start the application or could also be your windows service.
Step 2: Add a class library to the solution. This will be used to define the web api service.
Step 3: Add the nuget package. Open the nuget manager and search for Unity.SelfHostWebApiOwin.
The console application also requires the Owin assemblies, if it is a separate project.
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" /> <package id="Microsoft.Owin.Host.HttpListener" version="2.1.0" targetFramework="net45" /> <package id="Microsoft.Owin.Hosting" version="2.1.0" targetFramework="net45" /> <package id="Owin" version="1.0" targetFramework="net45" /> </packages>
Step 4: Add your unity registrations as required, in Unity.SelfHostWebApiOwin/UnityHelpers
public static void RegisterTypes(IUnityContainer container) { // Add your register logic here... var myAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("HelloWorldUnityWithSelfHostedOwin")).ToArray(); container.RegisterType(typeof(Startup)); container.RegisterTypes( UnityHelpers.GetTypesWithCustomAttribute<ContainerControlledAttribute>(myAssemblies), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled, null ).RegisterTypes( UnityHelpers.GetTypesWithCustomAttribute<TransientLifetimeAttribute>(myAssemblies), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.Transient ); }
Step 5: Add your startup logic as required. The startup here is just a place holder. This must be replaced.
public static void StartServer() { string baseAddress = "http://localhost:8081/"; var startup = _container.Resolve<Startup>(); //options.ServerFactory = "Microsoft.Owin.Host.HttpListener" IDisposable webApplication = WebApp.Start(baseAddress, startup.Configuration); try { Console.WriteLine("Started..."); Console.ReadKey(); } finally { webApplication.Dispose(); } }
Step 6: Add your startup method to the console application.
using Unity.SelfHostWebApiOwin; namespace HelloWorldUnityWithSelfHostedOwin.Console { class Program { static void Main(string[] args) { Startup.StartServer(); } } }
Filter options
If custom filters are required and need to be injected, the unity build up is already configured. You only need to create the filter attributes classes. Here’s an example of a ActionFilter with property injection.
using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Filters; using HelloWorldUnityWithSelfHostedOwin.Business; using Microsoft.Practices.Unity; namespace HelloWorldUnityWithSelfHostedOwin.Filters { public class MyCustomFilterAttribute : ActionFilterAttribute { [Dependency] public IBusinessClass ServiceB { get; set; } public override void OnActionExecuting(HttpActionContext actionContext) { // pre-processing ServiceB.Hello(); } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { var objectContent = actionExecutedContext.Response.Content as ObjectContent; if (objectContent != null) { var type = objectContent.ObjectType; //type of the returned object var value = objectContent.Value; //holding the returned value } //DO Something } } }
The application can be tested using fiddler. A complete working example is provided on github.
Release Notes
Code example https://github.com/damienbod/HelloWorldUnityWithSelfHostedOwin
Code source : https://github.com/damienbod/SelfHostWebApiWithOwinAndUnityUsingNuget
Improvements for future releases
– Per request lifecycle
– AOP filter templates
Links
… [Trackback]
[…] Read More here: damienbod.wordpress.com/2013/10/12/self-hosted-web-api-using-owin-and-unity-nuget-package-documentation/ […]
I couldn’t resist commenting. Very well written!