Web Api 2, OWIN and StructureMap

Add the following code to your startup file:

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
        // setup http configuration 
        GlobalConfiguration.Configure(config => 
        { 
            //configure dependency injection 
            var container = IoC.Initialize(); config.ConfigureDependencyInjection(container); config.ConfigureControllerSelector(); 
        }
    }
} 

And create the following IoC container:

public static class IoC 
{ 
    public static IContainer Initialize() 
    { 
        var container = new Container(c => 
        { 
            c.AddRegistry(); 
            // TODO: Add custom registry here 
        }); 
        return container; 
    } 
}

And a WebApi Registry:

public class WebApiRegistry : Registry 
{ 
    public WebApiRegistry() 
    { 
        Scan( scan => 
        { 
            scan.TheCallingAssembly(); 
            scan.WithDefaultConventions(); 
        }); 
        For().Use(() => GlobalConfiguration.Configuration); 
    } 
}
comments powered by Disqus