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

后端 未结 2 853
野性不改
野性不改 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: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);
        } 
    }
    

提交回复
热议问题