3

I am trying to inherite a base global.asax class to create my custom global.asax class. But my custome inherited global class does not work properly. Its Application_Start wont been called.

Anyone knows whey?

public class BaseGlobal : HttpApplication
{
    protected void Application_Start(Object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure();
        Logger.Warn("Portal Started");  //I can find it log file
    }
    ......
}


public class MyGlobal : BaseGlobal
{
        new protected void Application_Start(Object sender, EventArgs e)
        {
            base.Application_Start(sender,e);

            Logger.Warn("Portal Started 2"); // Can not find it in log file
        }
}


<%@ Application Codebehind="Global.asax.cs" Inherits="Membership_ABC.MyGlobal" Language="C#" %>

In the log file, I could not find "Portal started 2" but just "Portal Started".

Any ideas?

ValidfroM
  • 2,626
  • 3
  • 30
  • 41
  • Does this compile ? the "new protected" seems weird, I think an override keyword should be welcomde – AlexH Nov 28 '12 at 12:42
  • 2
    It looks like someone slapped the `new` keyword on the to shut up the compiler. Well, that is the problem. – usr Nov 28 '12 at 12:45
  • 1
    AlexH - you are.exactly correct. The "new Modifier" hides the parents code in the child class. http://msdn.microsoft.com/en-us/library/435f1dw2.aspx – dana Nov 28 '12 at 12:46
  • usr do not think 'new' is the cause of that. using 'new' is just to silent the VS. – ValidfroM Nov 28 '12 at 12:56
  • 1
    @ValidfroM that's just what I said! You silenced VS thereby introducing a bug. Read up on what "new" does before applying it! – usr Nov 28 '12 at 13:25

2 Answers2

3

On startup of an application, the runtime takes the HttpApplication descendant that is pointed out by the Global.asax file, and creates an instance of it. The runtime does not know or care how the class got to be descended from HttpApplication, it just cares that it is, in fact a descendant.

After that it start calling method on it, treating it as a regular HttpApplication object. Since the new modifier effectively breaks the inheritance chain (it's just a new method that happens to share the old methods name) it is not called, but instead the method of the parent class is called. Basically you have this situation (pseudocode):

HttpApplication httpApp = new MyGlobal();
httpApp.Application_Start(..) 
// ^^^ calls BaseGlobal.Application_Start(..)
//because the is not an unbroken chain from HttpApplication to MyGlobal

This is an example and consequence of the Brittle Base Class problem, a topic about which Eric Lippert has written in depth.

SWeko
  • 30,434
  • 10
  • 71
  • 106
1

The solution is to declare the function virtual in the base class and then override it in the child class.

But as you can't edit base class to declare the Application_Start method virtual, it won't work : Is it possible to override a non-virtual method?

The accepted answer gives an example that matches your case.

Community
  • 1
  • 1
AlexH
  • 2,650
  • 1
  • 27
  • 35
  • I know ur solution can work, but I can not change the base class. About " If the base implementation is hidden; the HttpApplication instance used at runtime must be a BaseGlobal type and not a MyGlobal type", can you give a link or sth describe that.I thought once the baseGlobal is hidden, the runtime will call the "new" Application_Start – ValidfroM Nov 28 '12 at 13:03
  • No it won't call the method overriden with the new keyword in that case – AlexH Nov 28 '12 at 13:30