Search code examples
c#prismclickonce.net-4.8

Problem setting AppDomain.CurrentDomain.UnhandleException in ClickOnce


In one of my ClickOnce applications I get an System.MethodAccessException when I try to subscripe to AppDomain.CurrentDomain.UnhandledException

My initializations code look like this:

App.xaml:

<mylib:MyApplicationBase x:Class="MyApplication.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApplication"
    xmlns:mylib="clr-namespace:MyLib;assembly=MyLib"
>
</mydll:MyApplicationBase>

App.xaml.cs:

namespace MyApplication
{
    public partial class App : MyApplicationBase
    {
        public App()
        {
            var domain = AppDomain.CurrentDomain;
            // exception in next line
            domain.UnhandledException += OnUnhandledException;
        }

        public void OnUnhandledException(object sender, UnhandledExceptionEventArgs eventArgs)
        {
            // todo
        }
    }
}

and in another assembly: MyApplicationBase.cs

namespace MyLib
{
    public class MyApplicationBase : System.Windows.Application
    {
        // Some more code but no explicit constructor definitions
    }
}

The code run fine on same machine if not installed by ClickOnce

I use a similar approach in other applications without problems


Solution

  • I got the System.MethodAccessException instead of the underlying problem. Because the appliaction had Project Properties Security set to:

    • Enable ClickOnce security settings: checked
    • This is a full trust application: selected

    enter image description here

    So the approach I took was:

    • uncheck: Enable ClickOnce security settings
    • publish (to local folder)
    • install and run local to investigate
    • fix the now visible underlying error
    • check: Enable ClickOnce security settings
    • publish
    • install and run on target machine