OWIN middleware for structuremap

1,221 views
Skip to first unread message

Axel Charpentier

unread,
Jul 24, 2014, 4:45:58 AM7/24/14
to structure...@googlegroups.com
Hi, 

I am using StructureMap 3, on a webapi project with the package structuremap.webapi2.
As far as I understand the lifetime scope of the nested container is based on the Http request, which is great, but this scope is build upon on an IHttpModule which is not consistant with the new OWIN pipeline architecture, could we do something like this : 

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));

Please let me know what do you think

Cheers

Axel

Jeremy Miller

unread,
Jul 24, 2014, 8:57:00 AM7/24/14
to structure...@googlegroups.com
Axel,

The specifics of your code would not work, but what you're trying to do in concept is valid. I'm on record as saying that sharing an IoC container through OWIN middleware is unlikely to be a good idea though unless all the middleware is really part of the application for what that's worth.

1.) The nested container feature has no coupling to HttpContext whatsoever, and was purposely built in part to decouple StructureMap's lifecycle from the HttpContext.
2.) When you create the nested container or the Web API IDependencyScope, I think you'd need to stash it in the OWIN environment dictionary and all subsequent inner
     middleware would need to use that as its service locator in order to make the scoping work
3.) You need to dispose the nested container at the end of the middleware chain. The call to HttpContextLifecycle does nothing for you here.

- Jeremy

Axel Charpentier

unread,
Jul 25, 2014, 3:24:40 PM7/25/14
to structure...@googlegroups.com
Thank for your answer Jeremy, 

But I  was just wondering about hooking on OWIN instead of an IHTTPModule  in order to get rid of System.Web and to be compliant with the OWIN abastraction layer

The idea is to instanciate and dispose the NestedContainer using the StructureMapOWINMiddleware and to store it in the OWIN dictionary on a per request basis exactly like it is done in StructureMapDependencyScope

Beside of the above Middleware and the startup config, I just needed to change this in the StructureMapDependencyScope : 

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);
            }
        }

Axel

Jeremy Miller

unread,
Jul 29, 2014, 8:21:04 PM7/29/14
to structure...@googlegroups.com
So are you good to go? Is this for your own framework or code riding directly on top of OWIN? 

Axel Charpentier

unread,
Aug 2, 2014, 5:40:06 AM8/2/14
to structure...@googlegroups.com
Yes thank.

I have done that for my own site, so I guess the answer is riding directly on top of Owin :)
Reply all
Reply to author
Forward
0 new messages