在 Global.asax 上使用 IHttpModule

发布于 2024-07-14 16:11:34 字数 1515 浏览 7 评论 0原文

我接到了一项令人兴奋的任务:重写我们的异常处理系统。 虽然我要指出的是,从应用程序范围的角度处理异常不是我们想要的,但通常当我们的团队人手不足而无法完成我们需要推出的大量工作时,这是不可避免的,所以请不要煽动异常处理的全球化解决方案在这里:)

我已经很好地寻找了存在的常见解决方案。 目前,我们使用 Global.asax 和 Application_Error 事件来执行 Server.GetLastError() ,该服务器被置于会话状态,然后调用重定向到另一个页面,然后在该页面中检索会话数据并以人类可读的格式输出。 重定向还调用一个存储过程,该存储过程将仔细审核错误信息,这些信息 a) 通过电子邮件发送给开发人员,b) 从仅开发人员可见的网页中查看。

我看到的做事的新方法是使用 IHttpModule 接口,使用 App_Code 中的类来执行这些操作(这是我的快速实现)

Imports Microsoft.VisualBasic

Public Class ErrorModule : Implements IHttpModule

  Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    ' Not used
  End Sub

  Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.Error, AddressOf context_Error
  End Sub

  Public Sub context_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim ex As Exception = HttpContext.Current.Server.GetLastError

    ' do something with the error
    ' call the stored procedure
    ' redirect the user to the error page

    HttpContext.Current.Server.ClearError()
    HttpContext.Current.Response.Redirect("index.htm")

  End Sub
End Class

我的问题是,这个解决方案相对于使用 Global.asax 事件有什么好处? 此外,将数据传递到错误页面的最佳方法是什么?

编辑: 顺便说一句,上面的代码确实可以工作;)

编辑: 另外,HttpModule 在幕后如何工作? 它是否只是在应用程序启动时将错误事件注册到该特定函数?

更新:

经过进一步调查,在使用 IHttpModule 接口时,获取会话数据似乎真的非常混乱。 我认为 MS 的 HttpModule 还不够成熟,无法在我们的特定场景中使用 - 除非出现特定于会话数据的事件,否则使用它对我们来说太危险了。

I've been given the thrilling task of re-writing our exception handling system. Whilst I will state that handling exceptions from an application-wide point of view isn't something we want, typically it's unavoidable when our team are understaffed for the sheer amount of work we need to push out the door, so please, no flaming the globalised solution to exception handling here :)

I've had a good hunt to see what common solutions exist. At the moment we use Global.asax with the Application_Error event to do Server.GetLastError() which is placed in Session state then a redirect is called to another page where the session data is then retrieved and output in a human readable format. The redirect also calls a sproc which will carefully audit the error information which is a) e-mailed to the developers and b) viewed from a web page only viewable by developers.

The new way I've seen of doing things is using the IHttpModule interface using a class in App_Code to do something along these lines (this is my quick implementation)

Imports Microsoft.VisualBasic

Public Class ErrorModule : Implements IHttpModule

  Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    ' Not used
  End Sub

  Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.Error, AddressOf context_Error
  End Sub

  Public Sub context_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim ex As Exception = HttpContext.Current.Server.GetLastError

    ' do something with the error
    ' call the stored procedure
    ' redirect the user to the error page

    HttpContext.Current.Server.ClearError()
    HttpContext.Current.Response.Redirect("index.htm")

  End Sub
End Class

My question is, what is the benefit of this solution over using Global.asax events? Additionally, what is the best way to hand the data to an error page?

EDIT: The code above does work by the way ;)

EDIT: Also, how does the HttpModule work behind the scenes? Does it just register the Error event to that particular function on application start?

UPDATE:

Upon much further investigation it seems grabbing session data is really, really messy when it comes to using IHttpModule interface. I don't think MS have matured HttpModule enough for it to be used in our particular scenario - until there are events specific to session data it's too dangerous for us to use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-07-21 16:11:34

使用模块的优点是易于删除,禁用它所需要做的就是从中删除它。 在你的配置中。

就您的数据而言,请尝试使用 Server.Transfer Server.RewritePath -这将保留所有当前数据(包括最后的服务器错误)。

如果由于某种原因它清除了最后一个错误,您可以将错误保存到 HttpContext.Items 在传输/重写之前,然后检索它。

编辑:为了响应您的编辑,IHttpModule 会附加到其 IHttpModule.Init 实现。

Using a module has the advantage of being easily removable, all you need to do to disable it is to remove it from <httpModules> in your config.

As far as your data goes, try going with Server.Transfer or Server.RewritePath - that will keep all the current data (including the last server error).

If for some reason it clears the last error, you can save the error to HttpContext.Items before the transfer/rewrite and then retrieve it afterwards.

Edit: In response to your edit, an IHttpModule attaches to any appropriate events in it's IHttpModule.Init implementation.

你列表最软的妹 2024-07-21 16:11:34

HttpModule 基本上与 Global.asax 执行相同的操作。 它被设计为一个更可重用且独立的事件处理模块。

HttpModule basically does the same thing as Global.asax. It's designed as a more reusable and self-contained module for event handling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文