Using a service in the Pre_Init event handler

Topics: UIP Application Block discussion, User Forum
Sep 15, 2008 at 6:04 PM
Hi, I am trying to use an instance of a global service in the Pre_Init event in a page. The instance should be injected by object builder through a setter of a public property on te page's presenter. But it seems that at this point of time of the page lifecycle the dependency injection hasn't really taken place, since I get a null reference for the service instance. I am using the October 2007 release of WCSF. Is there some work around for this issue?
Thank you in advance!
Sep 15, 2008 at 8:02 PM
I believe, starting October 2007, DI is initiated in the Pre_Init event (Somebody correct this if I am wrong, but I think DI is initiated by the Page object, not the other way around, due to the fact that the hooks to do DI during object construction were not there. Somebody from the development team had a detailed article about that.)

There have been multiple posts/proposed work-arounds on the issue, one of which provides for a custom-extended WebApplication implementation that forces DI and you go from there. I would recommend you read up on that in this forum instead of me duplication all the info one more time here.

Giorgi
Sep 15, 2008 at 8:58 PM
Edited Sep 15, 2008 at 9:01 PM

Hi,

The OnPreInit() method of ASP .NET pages fire the page’s Pre_Init Event (For more information, read here). CWAB inherits from Page class of ASP .NET and override the OnPreInit() Method to enable dependency injection in the following way:

 

        protected override void OnPreInit(EventArgs e)

        {

                  base.OnPreInit(e);

                  WebClientApplication.BuildItemWithCurrentContext(this);

        }

 

If you use the Pre_Init event of your page, the dependency injection has not taken place yet. You should override the OnPreInit() method and call base.OnPreInit(e). After this point you will be able to use your global service or your presenter without problems because the dependency injection would have already taken place.

 

Please, let me know if you find it useful.

               

Ezequiel Sculli

http://blogs.southworks.net/esculli