Add "automated error reporting" to DLLs

MarcoMarco Posts: 19
edited July 21, 2010 3:38PM in SmartAssembly 5
Hi

The main part of our program is still in Visual Basic 6 and some smaller parts in .Net. The .Net parts are DLL assemblies. If I want to add the automated error reporting to my .Net DLLs, the SmartAssembly say:
Because this assembly is a library, these options will not work automatically. They will only work if accessed by an EXE that has been processed by SmartAssembly.
Is there a way to add the automated error reporting manually to DLL assemblies?

Thanks
Marco

Comments

  • Paul.MartinPaul.Martin Posts: 83 New member
    I think the best answer I can give is ....possibly.

    It is possible to get SmartAssembly to add error reporting to a DLL, none of which are particularly nice (as described below, it is not sensible for the vast majority of situations, so we discourage it).

    However, when I tried it for the situation you describe using COM between them it did not work. I'm not sure whether it is something to do with my test programs, or whether there is something on the way COM works for .NET is stopping it.

    You may get it to work though - I've given the instructions below.
  • Paul.MartinPaul.Martin Posts: 83 New member
    The problem with DLL is that they are intended to be used by an external application.
    In this schema, throwing exceptions in a must-have.

    For example, this is the responsibility of the DLL to throw an ArgumentException if a method is not properly called.
    And the caller (the .EXE) must handle the exception and behave accordingly.

    So, most of the exceptions must be simply report to the caller.

    For this very reason, it is not possible to report unhandled exception as a global practice.
    That's why the option is not available for DLL in {smartassembly}.

    Anyway, a couple of workaround is available for some kind of DLL (mainly web-services or add-ins) but it requires a few changes in the DLL code (and {smartassembly}'s SDK for the second one):

    1) Creating a DLL with an entry point.

    The trick is to create a Console App EXE instead of a DLL. The only difference between the two is that the Console App has an entry-point.
    You do nothing in the entry-point (the 'Main' method), but {smartassembly} will add its own code in this method.
    In the static constructor of your plug-in class, you call the entry-point.

    class Program
    {
    static void Main()
    {
    //empty
    }
    }

    class MyPlugin : IPlugin
    {
    static MyPlugin()
    {
    //ensure that {sa} initialization is executed
    Program.Main();
    }

    //your code here...
    }

    Then, you just have to rename the .EXE into a .DLL, and you have a DLL with an entry-point.

    2) Using SmartAssembly.ReportException.dll (Requires the Enterprise edition of {smartassembly}).
    You need to refer the dependency "C:\Program Files\{smartassembly}\SDK\bin\SmartAssembly.ReportException.dll" (this dependency will be removed when processed by {smartassembly}).

    Then, for any public method of your DLL that you want to protect, you need to do the following:
    Of course, you just need to do it for the "entry-points" of your DLL.

    using SmartAssembly.ReportException;

    public class MyClass
    {
    private void DoSomethingInternal()
    {
    //Your code here
    }

    public bool DoSomething()
    {
    try
    {
    DoSomethingInternal();
    return true;
    }
    catch (Exception exception)
    {
    ExceptionReporting.Report(exception);
    return false;
    }
    }
    }



    A couple of things to note:
    - if you use method 1, SmartAssembly has slightly different default obfuscation and pruning settings for applications and libraries (for applications the protection is generally slightly stronger).
    - if you use method 2, on SmartAssembly 4 you need to manually add option to enable the unhandled exception feature in the project file. If you open the "{sa}proj" file for the library (it is a simple XML file) you need to add the node "<ExceptionReporting ReportExceptions="1" />" under the "Options" section - e.g.
    <SmartAssemblyProject>
    <Configuration>
    .........
    <Options>
    ...........
    <ExceptionReporting ReportExceptions="1" />
    </Options>
    </Configuration>
    </SmartAssemblyProject>

    In SmartAssembly 5 there is an option in the UI to change the setting so you don't have to manually edit the project file.


    The other method you can use is similar to method 2; however, instead of adding a try/catch to every method, you manually add a handler to the framework's unhandled exception event.
    The handled exception event varies being different types of applications. For a WPF class library the main one of AppDomain.UnhandledException. So you will want to add something like:
    Public Sub AppDom_UnhandledExceptions(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs) Handles _curDomain.UnhandledException
    SmartAssembly.ReportException.ExceptionReporting.Report(CType(args.ExceptionObject, Exception))
    End Sub

    You will then need to add a static initialiser which links this event handler to the event.
Sign In or Register to comment.