locked
Global.asax bug? RRS feed

  • Question

  • User-1898157721 posted

    I noticed a very strange behavior of Global.asax events. I noticed that if event like Application_BeginRequest is defined in code behind for Global.asax (Global.asax.cs) and Global.asax itself, then Application_BeginRequest would fire twice, but both times from Global.asax.

    Here is a simple example to illustrate what I'm talking about.

    Global.asax.cs

        
        public class MainGlobal : System.Web.HttpApplication
        {
            public void Application_BeginRequest(object sender, EventArgs e)
            {
                Response.Write("1");
            }
        }
    
        public class Global : MainGlobal
        {
            public new void Application_BeginRequest(object sender, EventArgs e)
            {
                Response.Write("2");
            }
        }
    


    Global.asax

    <script Language="C#" RunAt="server">
        public void Application_BeginRequest(object sender, EventArgs e)
        {
            Response.Write("3<br>");
        }
    </script> 
    In this this case output, will be 333. If I comment out code from Global.asax output is going to be 22, etc. 
    It seems like ASP.NET correctly determines that there are 3 Application_BeginRequest are defined, but it always calls the same method.
    It totally seems like a bug in ASP.NET. I think expected behavior here should be: 1) 123 or 2) exception saying that you can't have multiple Application_BeginRequest
     

    Victor - Software Developer
    Personify Design
    Free Image Converter

    Tuesday, February 2, 2010 4:45 PM

Answers

  • User1616291716 posted

    It is a very strange behavior. I can't imagine that it is by design. I think it's a bug in how Global.asax works.

     

    As far as I know, the behavior is not bug, please refer to the following points.

    Generally, when you add a Global file to your project and add the following Code to the Global.asax.

     void app_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            app.Response.Write("d");
        }


    The above method indicate that you subscrible the BeginRequest event of HttpApplication(Not method). However, when you try to customize the class (global.cs). How many you declare the event as long as how many the event will be fired. As wel know, all the event will be added to EventHanderList object and fired orderly. So I suggest you do as follows to instead keyword('New').

    public class MainGlobal : System.Web.HttpApplication
    {
         
    }
    
    public class Global : MainGlobal
    {
        public Global()
        {
    
        }
        public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            base.BeginRequest -= null;
            Response.Write("2");
        }
    }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 7, 2010 10:53 PM

All replies

  • User1611391320 posted

    strange behaviour

    Wednesday, February 3, 2010 12:14 AM
  • User-1898157721 posted

    It is a very strange behavior. I can't imagine that it is by design. I think it's a bug in how Global.asax works.

    Wednesday, February 3, 2010 5:36 PM
  • User1616291716 posted

    It is a very strange behavior. I can't imagine that it is by design. I think it's a bug in how Global.asax works.

     

    As far as I know, the behavior is not bug, please refer to the following points.

    Generally, when you add a Global file to your project and add the following Code to the Global.asax.

     void app_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            app.Response.Write("d");
        }


    The above method indicate that you subscrible the BeginRequest event of HttpApplication(Not method). However, when you try to customize the class (global.cs). How many you declare the event as long as how many the event will be fired. As wel know, all the event will be added to EventHanderList object and fired orderly. So I suggest you do as follows to instead keyword('New').

    public class MainGlobal : System.Web.HttpApplication
    {
         
    }
    
    public class Global : MainGlobal
    {
        public Global()
        {
    
        }
        public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            base.BeginRequest -= null;
            Response.Write("2");
        }
    }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 7, 2010 10:53 PM
  • User-1498334129 posted

     It is kind of polymorphic behaviour because of multiple classes and events getting wired automatically.

    Sunday, February 7, 2010 11:23 PM
  • User-1898157721 posted

    I still don't understand why you think it's not a bug. I understand why it fires event twice, but I would expect it fire different events instead of one event twice.


    It seems like it does

    class A { public void x() {} }

    class B : A {public new void x {} }

    A a = new A();

    B b = new A();

    o.event += new EventHandler(b.x);

    o.event += new EventHandler(b.x);

    instead of

    o.event += new EventHandler(a.x);

    o.event += new EventHandler(b.x);

    Wednesday, February 10, 2010 8:51 PM
  • User-1898157721 posted

     It is kind of polymorphic behaviour because of multiple classes and events getting wired automatically.

    I would agree with you if methods would be virtual, but they are not.

    Wednesday, February 10, 2010 8:55 PM
  • User401360897 posted

    http://forums.asp.net/p/1533933/3724875.aspx#3724875

    Thursday, March 11, 2010 12:38 AM