Skip to content

Instantly share code, notes, and snippets.

@Saanch
Created July 16, 2015 01:41
Show Gist options
  • Save Saanch/8640d6941ec53f16be50 to your computer and use it in GitHub Desktop.
Save Saanch/8640d6941ec53f16be50 to your computer and use it in GitHub Desktop.
structuremap OWIN middleware to handle the container per owin context
StructureMapOWINMiddleware in remplacement of the IHttpModule :
//basic draft
public class StructureMapOWINMiddleware : OwinMiddleware
{
public StructureMapOWINMiddleware(OwinMiddleware next)
: base(next)
{
}
public async override Task Invoke(IOwinContext context)
{
IoC.DependencyScope.CreateNestedContainer(); // I moved de dependencyScope static in the IOC Class
await Next.Invoke(context); // Process the request with all other OWIN Middleware
HttpContextLifecycle.DisposeAndClearAll(); //clean all
IoC.DependencyScope.DisposeNestedContainer();
}
}
and in the startup class, on the first line to be sure the IOC is configured before everthing else :
IContainer container = IoC.Initialize();
app.Use(typeof(StructureMapOWINMiddleware));
public IContainer CurrentNestedContainer
{
get
{
if (OwinContext == null) //OwinContext is passed in the ctor
return null;
return OwinContext.Get<IContainer>(NestedContainerKey);
}
set
{
OwinContext.Set<IContainer>(NestedContainerKey, value);
}
}
more discussion is at https://groups.google.com/forum/#!topic/structuremap-users/EX3MmHsFNY8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment