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.