views:

433

answers:

2

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.

+1  A: 

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

Steven Sudit
Unhelpfully enough, sn.exe spits this out when I try to sign it:>sn -R "C:\Documents and Settings\zchoy\My Documents\Burrow\Code\Main\lib\Elmah.dll" "C:\documents and settings\zchoy\my documents\burrow\code\code signing key.pfx"Microsoft (R) .NET Framework Strong Name Utility Version 3.5.30729.1Copyright (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
Zian Choy
I think you need to use al, not sn, to sign it, although you will need to generate the key with sn first. For instructions, look at http://msdn.microsoft.com/en-us/library/xc31ft41.aspx
Steven Sudit
Those instructions talk about how to create a signed assembly out of one or more modules, and how to work with assemblies that were flagged for delay-signing at compilation time, but don't seem to say anything about how to sign an already-existing assembly that was compiled without a strong name.
Joel Mueller
+2  A: 

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.

Joel Mueller
This is fine if you have source code. In general, al.exe is the solution.
Steven Sudit
Can you detail the exact syntax for using al.exe to sign a compiled assembly (not a module) that is not flagged for delay-signing? I can't make it work, and I don't see anything in the documentation for al.exe that makes me think it should work. In the past I've had to resort to ILDASM/ILASM to get a signed version of an assembly to which I did not have source code, and if al.exe worked for that, it would have been much simpler.
Joel Mueller
@Joel: According to http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic30397.aspx, it's more accurate to say that al.exe lets you create a new assembly that's signed, rather than signing the existing one.
Steven Sudit
I agree, Steven. That thread you linked to also says, "You can't sign an existing unsigned assembly with Al.exe." You seem to be saying the opposite, which would be very convenient, which is why I asked for an example.
Joel Mueller