StructureMap and ASP.net 5 VNext

I've created a working example of how to integrate StructureMap with ASP.net 5. IoC in VNext comes out of the box but if you want to bring your own container you can.

Below you can find a full working example of how to integrate with MVC 6.

https://github.com/aaron-hammond/StructureMap.VNext


EDIT: I removed this repo, as it got stale,  proper support has been created here:

https://github.com/structuremap/structuremap.dnx

Shouldn't need to roll our own now:

MVC6 and the IHttpContextAccessor

I've been working a lot with ASP.net 5 recently using the RC of visual studio 2015. There is considerable changes in both the .net framework and of course MVC.

One of the most significant changes is the dependency of APS.net 5 on the System.Web DLL. This DLL, that pretty much underpinned all that was web development, has been broken up into independent granular nuget packages that allow you to ship your application with exactly what you need.

Applications that we've used in the past have relied heavily on the HttpContext class allowing us to access the current context by the static property HttpContext.Current.

In ASP.net 5 large parts of the framework are now Dependency Inject-able. In order to access the current context you can simply inject the IHttpContextAccessor into your class in order to access the current context.


The beauty of this is we can then create our mocked HttpContext using the DefaultHttpContext object that can be found in Microsoft.AspNet.Http.Core.dll




Don't forget all .net code is now open sourced and this particular interface can be found at:

https://github.com/aspnet/Hosting/blob/release/src/Microsoft.AspNet.Hosting.Abstractions/IHttpContextAccessor.cs

The interface has also been moved to:

https://github.com/aspnet/HttpAbstractions/tree/release/src/Microsoft.AspNetCore.Http.Abstractions

Update:

As of RC2 some changes have been made to the IHttpContextAccessor and it's availability. The announcement on this change can be found here:

https://github.com/aspnet/Announcements/issues/190