gpt4 book ai didi

c# - 从供应商的 Global.asax 继承 Global.asax 时,Application_Start 不会触发

转载 作者:行者123 更新时间:2023-11-30 12:45:01 30 4
gpt4 key购买 nike

我正在尝试将代码添加到我没有源代码的第 3 方 ASP.NET Web 应用程序的 application_start 事件。我通过从供应商的 Global 类继承来做到这一点。这是代码(注意“new”关键字和对事件的基类版本的两次调用。这是因为基类方法不是抽象的、虚拟的或覆盖的,我没有源代码来改变它) :

public class CustomGlobal : VendorNamespace.Global
{
new protected void Application_Start(object sender, EventArgs e)
{
var logName = "SPPCustom";
if (!System.Diagnostics.EventLog.SourceExists(logName))
System.Diagnostics.EventLog.CreateEventSource(logName, "Application");
var text = "Hello from Application Start!";
System.Diagnostics.EventLog.WriteEntry(logName, "Application started!");
File.WriteAllText(@"c:\ApplicationStart.txt", text);
Debug.WriteLine(text);
base.Application_Start(sender, e);
}
new protected void Session_Start(object sender, EventArgs e)
{
var logName = "SPPCustom";
if (!System.Diagnostics.EventLog.SourceExists(logName))
System.Diagnostics.EventLog.CreateEventSource(logName, "Application");
System.Diagnostics.EventLog.WriteEntry(logName, "Session started!");
base.Session_Start(sender, e);
}
}

我在站点的 global.asax 文件中引用我的代码,如下所示:

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

Application_Start 代码不执行,但 Session_Start 代码执行。我可以从 Session_Start 编写事件并写出文本文件,但 Application_Start,nadda。

有人知道这里发生了什么吗?

编辑:

这是实现 Jan 的建议后的代码:公共(public)类 CustomGlobal:HttpApplication { 私有(private)只读全局_global; 私有(private)只读方法信息_appStartInfo; 私有(private)只读方法信息_sessionStartInfo;

    public CustomGlobal()
{
_global = new Global();
_appStartInfo = typeof(Global).GetMethod("Application_Start", BindingFlags.Instance | BindingFlags.NonPublic);
_sessionStartInfo = typeof(Global).GetMethod("Session_Start", BindingFlags.Instance | BindingFlags.NonPublic);
}

protected void Application_Start(object sender, EventArgs e)
{

var logName = "SPPCustom";
if (!System.Diagnostics.EventLog.SourceExists(logName))
System.Diagnostics.EventLog.CreateEventSource(logName, "Application");
var text = "Hello Patient Portal from Application Start!";
System.Diagnostics.EventLog.WriteEntry(logName, "Application started!");
File.WriteAllText(@"c:\PatientPortalApplicationStart.txt", text);
Debug.WriteLine(text);
//_sxaGlobal.ApplicationStart(sender, e);
_appStartInfo.Invoke(_global, new[] {sender, e});
}
}

现在它抛出以下错误:

[NullReferenceException: Object reference not set to an instance of an object.]

Global.Application_Start(Object sender, EventArgs e) +28

[TargetInvocationException:调用目标抛出异常。] System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo 方法、对象目标、对象[] 参数、SignatureStruct& sig、MethodAttributes methodAttributes、RuntimeType typeOwner)+0 System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo 方法、对象目标、对象 [] 参数、签名 sig、MethodAttributes methodAttributes、RuntimeType typeOwner)+72 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo 文化, Boolean skipVisibilityChecks) +251 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19 CustomGlobal.Application_Start(Object sender, EventArgs e) +231

[HttpException (0x80004005): 调用目标抛出异常。] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext 上下文,HttpApplication 应用程序)+9239341 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +131 System.Web.HttpApplication.InitSpecial(HttpApplicationState 状态,MethodInfo[] 处理程序,IntPtr appContext,HttpContext 上下文)+194 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext 上下文)+339 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253

[HttpException (0x80004005): 调用目标抛出异常。] System.Web.HttpRuntime.FirstRequestInit(HttpContext 上下文)+9157968 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext 上下文)+97 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext 上下文)+256

最佳答案

这是继承问题。当使用 new 关键字时,调用 CustomGlobal.Application_Start() 将执行您的代码,但调用 ((HttpApplication) CustomGlobal).Application_Start()将执行供应商的代码。

解决方案可以是私有(private)成员的模式

public class CustomGlobal : HttpApplication
{
private readonly VendorGlobal _global;
private readonly MethodInfo _appStartInfo;

public CustomGlobal()
{
_global = new VendorGlobal();
_appStartInfo = typeof(VendorGlobal).GetMethod("Application_Start", BindingFlags.Instance | BindingFlags.NonPublic);
}

protected void Application_Start(object sender, EventArgs e)
{
_appStartInfo.Invoke(_global, new[] {sender, e});
// your custom code
}

关于c# - 从供应商的 Global.asax 继承 Global.asax 时,Application_Start 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873108/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com