Presenter not instantiated from ObjectBuilder

Topics: User Forum
Apr 25, 2008 at 1:30 PM
Hi, i am relatively new to implementing web apps with the web client software factory. I am developing an application that makes extensive use of ASP.NET personalization block as well as AJAX Control toolkig and would like to make it more modular, follow the MVP patter and use the PageFlow block from the web client software factory. Unfortunately I am stuck on my first view with presenter. The issue is that when the view is requested, i get a NullReferenceException in the PageLoad event because the presenter instance is null. Here is the scenario:
I have extended the WebClientApplicaiton class in order to make some validation check for certain Application scoped values. I have overriden the ApplicationStart method to fetch some data and then call the base implementation. In addition there is an Application_BeginRequest event handler where i do the check and if it fails i forward to a certain page by invoking the Server.Transfer() method. But the request fails with the given exception. The view is addded with the recipe Add view (with presenter). I am using the May 2007 release with VS 2005. The web.config file includes the configuration for the module with the presenter. The view itself is placed in a subfolder callsed Admin. the virtual paths attrubute of the module element is set to point to this folder.
Can someone please give me some hint of what am I doing wrong.
Thank you in advance!
Apr 25, 2008 at 3:32 PM
Make sure that the first thing you do in those over-ridden methods is call the base version. There is a lot of magic that happens in application start.

Michael Puleio - patterns & practices
Apr 25, 2008 at 5:28 PM

The problem is caused because you are transferring the execution to another page once the dependency injection has done (in the first requested page), after that, the transferred page doesn’t have the presenter injected.

In the new releases of WCSF (from October 2007 release, which works on VS2005, or February 2008 release which works on VS2008) does not appear this problem since the dependency injection is done in the Page_Load event.

Also, you can override the method Application_PreRequestHandlerExecute that is when the dependcy injection is done, and made the validation before calling the base implementation. Use Response.Redirect in order to get recalled the event and the dependency injection work with the right context.
protected override void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    if ( !Validate() )
        base.Application_PreRequestHandlerExecute(sender, e);

Please let me know if this helps.

Jonathan Cisneros
Apr 28, 2008 at 8:45 AM
Thanks for the replies guys. jonatan, I tried to find the october 2007 release, but with no success. In the releases section of the WCSF projet site, i get either the June 2007 or the v 2.0 from Feb 08. i saw that the june release was updated as late as feb 08, so i downloaded it and ran a Repair installation. However, the behaviour is still the same. I am adopting the second tip of yours, but would very much like to get hold of this release you've mentioned. Thanks in advance once more!
Apr 28, 2008 at 9:04 PM

cyberbyber wrote:
However, the behaviour is still the same.

I haven't verified this but it appears that the Server.Transfer() method does not fire the OnPreInit() event - herein lies the problem as this is where the process currently resides (in the CompositeWeb.Web.UI.Page baseclass) to execute the objectbuilder's dependency injection:

Microsoft.Practices.CompositeWeb.Web.UI.Page baseclass for views.
///<param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
protected override void OnPreInit(EventArgs e)
	WebClientApplication.BuildItemWithCurrentContext(this);   // <<<==== ObjectBuilder which processes [CreateNew] attribute

If this is correct then pages/views that are being transferred to will have to take necessary steps to ensure the WebClientApplication.BuildItemWithCurrentContext(this); gets executed.
Apr 29, 2008 at 12:43 AM
Edited Apr 29, 2008 at 12:44 AM
The October release was poorly named. Sorry about that. You can find just the guidance package bits in Composite Web Client Automation 2007.12.4.3. Or you can grab it from the MSDN Web Client Software Factory page under Guidance Assets - For .NET Framework 2.0.

Of course, if what BillKrat says about OnPreInit not firing is true, this could be a challenge. As a quick sanity check, what is the base class (fully qualified with the namespace) for your Pages? If it is System.Web.Page, then try changing it to Microsoft.Practices.CompositeWeb.UI.Page (or something very close to that; I can't recall it exactly).

I hope that helps,
Michael Puleio - patterns & practices