If you need something in StructureMap, but you can’t build it with new()…

I’ve had 3 separate questions in the past week about this, so I figured it’s blogworthy.  You’ll have to download the latest bits from svn at http://sourceforge.net/projects/structuremap.  Anyway, let’s say you want the HttpContext in StructureMap so that it can be injected into other classes.  The current HttpContext doesn’t even exist at configuration time, and you certainly can’t just say new HttpContext(), so we needed a way to tell StructureMap a way to fetch this object and bypass the traditional object building pipeline.  About 4 months ago I added this syntax to the trunk:

StructureMapConfiguration.BuildInstancesOf<HttpContextBase>.TheDefaultIs(
	ConstructedBy<HttpContextBase>(() => HttpContext.Current))
);
Doing this just enables StructureMap to poke the correct HttpContext into something like "public Controller(HttpContext context)." 
This strategy should be useful for remoted objects, Provider classes, the HttpContext, and whatever else you can dream up. 
Comments, and suggestions for improving the syntax (please), would be welcome. 

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://Alikor.co.uk Madu Alikor

    The correct Syntax is

    ForRequestedType().TheDefault.Is.ConstructedBy(() => new HttpContextWrapper(HttpContext.Current));

    the problem with using

    For().Use(new HttpContextWrapper(HttpContext.Current));

    is that the instance that is that the HttpContextWrapper will be created only once for the application and if you attempt to access the SessionState property it will be null

  • RickB

    This is the latest syntax:

    For().Use(new HttpContextWrapper(HttpContext.Current));

  • http://codebetter.com/members/jmiller/default.aspx Jeremy D. Miller

    @Travis,

    Yup. That should do it.

  • http://www.heliosfx.com Travis

    I’m assuming this is the latest syntax for this:

    ForRequestedType().TheDefault.Is.ConstructedBy(() => new HttpContextWrapper(HttpContext.Current));

  • Boris

    Just got latest (26-MAR-2008)
    I think I’m doing right but all I’m getting is:
    The name ‘ConstructedBy’ does not exist in the current context …

  • http://flux88.com Ben Scheirman

    I should let you know that that sample doesn’t actually work b/c you can’t cast HttpContext to HttpContextBase, as they didn’t alter HttpContext to inherit from it.

    You _can_ do this:

    return new HttpContextWrapper2( HttpContext.Current ) …. however given the name and some recent comments from PhilHa on twitter, I’m not sure I should be using this class directly.

    Ah well, works for the time being!

  • http://weblogs.asp.net/fbouma FransBouma

    (offtopic) you forgot to close the pre tag after the codesnippet (so the paragraph below it isn’t readable)