How to set up injection for HttpContextBase in MVC project

244 views
Skip to first unread message

Cory Neale

unread,
Jan 19, 2016, 6:02:30 AM1/19/16
to structuremap-users
I have an MVC project and i'm trying to set up StructureMap so I can inject the HttpContext into the controller, but I cannot work out how to do this.

I've done plenty of searching, and I keep coming across pages and forum posts telling me to do something like this:

ForRequestedType<HttpContextWrapper>().TheDefaultIs(new HttpContextWrapper(HttpContext.Current));

The problem I'm having with that is that I can't find a ForRequestedType method anywhere.  The posts I'm finding appear to be a few years old, so i'm guessing they are from an older version of StructureMap.

I have installed StructureMap using the structuremap.mvc5 nuget package, which installed v3.1.1.0 of StructureMap.

Can anyone advise me on how to correctly map HttpContext?

Thanks,

Cory.


Jeremy D. Miller

unread,
Jan 19, 2016, 7:38:34 AM1/19/16
to structure...@googlegroups.com
That wouldn't have worked out very well anyway because you need to resolve it lazily to be getting the current HttpContext. With SM 3/4 syntax, that would be:

For< HttpContextWrapper>(). Use(() => new HttpContextWrapper( HttpContext.Current));

The latest docs are at http://structuremap.github.io.
 


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/797d6a04-aade-4d13-8d2c-20947dbef3f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Cory Neale

unread,
Jan 20, 2016, 5:28:45 AM1/20/16
to structuremap-users, jeremy...@yahoo.com
Hi Jeremy,

Thanks, I've managed to get it all working now.

I had actually come up with the correct code while I was tinkering away trying to get it to work last night.  The reason I couldn't get it to work was that I was calling container.AssertConfigurationIsValid() after creating the container.

Anyway, thanks again.  I appreciate the help.

Cory.
Reply all
Reply to author
Forward
0 new messages