Unity and ASP.Net UserControls and MasterPages

Dec 1, 2010 at 12:27 PM


I'm trying to use unity in a web project since I quite liked it  in recent WPF and Silverlight Projects. I set it up as described in the Hands on Lab (Global.asax -Application_PreRequestHandlerExecute).

This works fine as long as my dependencies are on the .aspx Page, but if I add an property marked with the DependencyAttribute to the MasterPager or a .ascx UserControl which is embedded on my page the dependencies wont be resolved.

I'm guessing it has something to do with the way ASP.Net builds the objects. Is there a workaround for this problem or did I forget/miss something?

Dec 2, 2010 at 5:11 AM

Since ASP.NET is actually creating the pages and controls, the container doesn't (and can't) get invoked at creation time.

You'd need to write a little code that spun through the Page's Controls collection and called container.BuildUp on each one. We had written a proof-of-concept HttpModule that did this automatically, but apparently there's some issues with it right now.


Dec 2, 2010 at 8:41 AM

Thanks for your reply, it may not be what I hoped for, but it works. I'm a bit disapointed that this wasn't even in a sidenote in the hands on labs, since I think this is a quite common scenario. Hopefully it will get added in the future.