在 VB 中将 ELMAH 与 ASP.NET MVC 结合使用:编译:Elmah 没有强名称

发布于 2024-08-02 13:30:26 字数 2509 浏览 4 评论 0原文

我已经通读了将 ASP.NET MVC HandleError 错误传递给 ELMAH 的帖子和代码,并将代码转换为 VB:

Imports System
Imports System.Web
Imports System.Web.Mvc
Imports Elmah

Public Class HandleErrorAttribute
    Inherits System.Web.Mvc.HandleErrorAttribute
    Public Overrides Sub OnException(ByVal context As ExceptionContext)

        MyBase.OnException(context)

        Dim e As Exception = context.Exception
        If Not context.ExceptionHandled OrElse RaiseErrorSignal(e) OrElse IsFiltered(context) Then
            ' if unhandled, will be logged anyhow
            'prefer signaling, if possible
            'filtered?
        Else
            LogException(e)
        End If
    End Sub

    Private Function RaiseErrorSignal(ByVal e As Exception) As Boolean
        Dim context = HttpContext.Current
        If context Is Nothing Then Return False

        Dim signal = ErrorSignal.FromContext(context)
        If signal Is Nothing Then Return False

        signal.Raise(e, context)
        Return True
    End Function

    Private Function IsFiltered(ByVal context As ExceptionContext) As Boolean
        Dim config As ErrorFilterConfiguration = context.HttpContext.GetSection("elmah/errorFilter")

        If config Is Nothing Then Return False

        Dim testContext = New ErrorFilterModule.AssertionHelperContext(context.Exception, HttpContext.Current)

        Return config.Assertion.Test(testContext)
    End Function

    Private Sub LogException(ByVal e As Exception)

        Dim context = HttpContext.Current
        ErrorLog.GetDefault(context).Log(New Elmah.Error(e, context))
    End Sub

End Class

但是,我注意到当我尝试编译代码时,我从 VS2008 收到以下错误:

Error  3   Unable to emit assembly: Referenced assembly 'Elmah' does not have a strong name    Main

现在, HandleErrorAttribute.vb 位于 [包含 SLN 文件的文件夹]\Main\HandleErrorAttribute.vb 中,视图、控制器等均位于 Main 文件夹下。

如果您能够使原始 C# 代码正常工作,您是如何解决编译时错误的? (而且,如果你让它在 VB 中工作,那就更好了)

编辑

我已经尝试过用 sn.exe 对其进行签名:

C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -R "C:\Documents and Settings
\zchoy\My Documents\Burrow\Code\Main\lib\Elmah.dll" "C:\documents and settings\z
choy\my documents\burrow\code\code signing key.pfx"

Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

C:\Documents and Settings\zchoy\My Documents\Burrow\Code\Main\lib\Elmah.dll does
 not represent a strongly named assembly

显然没有帮助。

I've read through the posts and code for passing ASP.NET MVC HandleError errors to ELMAH and converted the code to VB:

Imports System
Imports System.Web
Imports System.Web.Mvc
Imports Elmah

Public Class HandleErrorAttribute
    Inherits System.Web.Mvc.HandleErrorAttribute
    Public Overrides Sub OnException(ByVal context As ExceptionContext)

        MyBase.OnException(context)

        Dim e As Exception = context.Exception
        If Not context.ExceptionHandled OrElse RaiseErrorSignal(e) OrElse IsFiltered(context) Then
            ' if unhandled, will be logged anyhow
            'prefer signaling, if possible
            'filtered?
        Else
            LogException(e)
        End If
    End Sub

    Private Function RaiseErrorSignal(ByVal e As Exception) As Boolean
        Dim context = HttpContext.Current
        If context Is Nothing Then Return False

        Dim signal = ErrorSignal.FromContext(context)
        If signal Is Nothing Then Return False

        signal.Raise(e, context)
        Return True
    End Function

    Private Function IsFiltered(ByVal context As ExceptionContext) As Boolean
        Dim config As ErrorFilterConfiguration = context.HttpContext.GetSection("elmah/errorFilter")

        If config Is Nothing Then Return False

        Dim testContext = New ErrorFilterModule.AssertionHelperContext(context.Exception, HttpContext.Current)

        Return config.Assertion.Test(testContext)
    End Function

    Private Sub LogException(ByVal e As Exception)

        Dim context = HttpContext.Current
        ErrorLog.GetDefault(context).Log(New Elmah.Error(e, context))
    End Sub

End Class

However, I noticed that when I try to compile the code, I get the following error from VS2008:

Error  3   Unable to emit assembly: Referenced assembly 'Elmah' does not have a strong name    Main

Right now, HandleErrorAttribute.vb lives in [folder with the SLN file]\Main\HandleErrorAttribute.vb and the Views, Controllers, and so on are all under the Main folder.

If you were able to get the original C# code to work, how did you get around the compile-time error? (and, if you got it to work in VB, that's even better)

Edit

I've already tried signing it with sn.exe:

C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -R "C:\Documents and Settings
\zchoy\My Documents\Burrow\Code\Main\lib\Elmah.dll" "C:\documents and settings\z
choy\my documents\burrow\code\code signing key.pfx"

Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

C:\Documents and Settings\zchoy\My Documents\Burrow\Code\Main\lib\Elmah.dll does
 not represent a strongly named assembly

Clearly unhelpful.

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

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

发布评论

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

评论(2

清风疏影 2024-08-09 13:30:26

当我遇到这个问题时,我下载了 ELMAH 源代码并在视觉工作室。然后我使用项目属性上的“签名”选项卡对程序集进行签名,然后编译我自己的 Elmah.dll 版本。

然后我将这个签名版本链接到我的主项目中。

When I had this problem, I downloaded the ELMAH source code and opened it up in Visual Studio. Then I used the Signing tab on the project properties to sign the assembly, then compiled my own version of Elmah.dll.

Then I linked this signed version into my main project.

岁月无声 2024-08-09 13:30:26

看起来您需要签署 Elmah 程序集,以便它具有强大的名称。

Looks like you need to sign the Elmah assembly so that it has a strong name.

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