我正在尝试继承一个基本的 global.asax 类来创建我的自定义 global.asax 类。但是我的客户继承的全局类不能正常工作。它的 Application_Start 不会被调用。
有人知道乳清吗?
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#" %>
在日志文件中,我找不到“Portal started 2”,只有“Portal Started”。
有什么想法吗?
最佳答案
在应用程序启动时,运行时采用 Global.asax
文件指出的 HttpApplication
后代,并创建它的一个实例。运行时不知道也不关心该类是如何从 HttpApplication
派生的,它只关心它实际上是一个派生对象。
之后它开始对其调用方法,将其视为常规 HttpApplication
对象。由于 new
修饰符有效地打破了继承链(它只是一个恰好共享旧方法名称的新方法)它不会被调用,而是调用父类的方法。基本上你有这种情况(伪代码):
HttpApplication httpApp = new MyGlobal();
httpApp.Application_Start(..)
// ^^^ calls BaseGlobal.Application_Start(..)
//because the is not an unbroken chain from HttpApplication to MyGlobal
这是 Brittle Base Class 问题的一个示例和结果,Eric Lippert 关于这个主题 has written in depth .
关于c# - 继承 Global.asax 和 Application_Start 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13605230/