Why can't my host (softsyshosting.com) support BeginRequest and EndRequest event handlers?

后端 未结 2 852
野性不改
野性不改 2021-01-12 01:00

I heard good things about Softsys Hosting and so I decided to move my ASP.NET MVC solution over to them. But it would not run on them. I was able to pinpoint the problem t

相关标签:
2条回答
  • 2021-01-12 01:45

    Looks to me like you went from IIS 6 or IIS 7 Classic mode to IIS 7 Integrated mode. In IIS 7 integrated mode, the Request processing was decoupled from application start. This article explains the why's and wherefores.

    To fix it, you'll need to move your code to Application_BeginRequest instead.

    0 讨论(0)
  • 2021-01-12 01:46

    You need register your handlers in each HttpApplication instance. There may be several pooled instances of HttpApplication. Application_Start is called only once (for IIS 6 and IIS 7 in classic mode - on the first request, for IIS 7 integrated mode - on web app start, just before any request). So to get all working you need to add events handlers in overrided Init method of HttpApplication or in constructor of it. If you add them in constructor - these handlers will be invoked first, even before the handlers of registered modules.
    So your code should look like this:

    public class MySmartApp: HttpApplication{
        public override void Init(){
            this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
            this.EndRequest += new EventHandler(MvcApplication_EndRequest);
        }
        protected void Application_Start(){
            RegisterRoutes(RouteTable.Routes);
        } 
    }
    

    or like this:

    public class MySmartApp: HttpApplication{
        public MySmartApp(){
            this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
            this.EndRequest += new EventHandler(MvcApplication_EndRequest);
        }
        protected void Application_Start(){
            RegisterRoutes(RouteTable.Routes);
        } 
    }
    
    0 讨论(0)
提交回复
热议问题