Question

When starting up my application on one machine it immidiately exits saying it has "stopped working". In the event log I see a MissingMethodException being the cause. There is no exception dialog displayed, and in the event log details I can't see any detailed error message (containing e.g "Method not found: xyz").

The event handler information says (tried to translate this back to english here, may not be verbatim)

Application: Myapp.exe Framework-version v4.0.30319

Description: The process was terminated due to an unhandled exception

Exception information: System.MissingMethodException

Stack: at MyApp.MainClass.Main(System.String[])

How can I find which method was missing when this exception was raised? Its a managed application but it has a number of native dependencies.

Edit: The compiled assemblies are all compiled in the same build, i.e. there are no version mismatches within the managed code of the application. It is possible that there is a mismatch in a binary dependency but if so, how can I find out which one?

It is a windows Forms application built in VS 2012 but targeting 4.0. The error is not raised on all machines with only framework 4.0 so that does not seem to be an issue.

Was it helpful?

Solution

The MissingMethodException tells you what method is missing with its Message property. Giving it a chance to tell you is however often overlooked by programmers. You must write an event handler for the AppDomain.UnhandledException event and display or log the e.UnhandledException object.

Do note that your program crashes very early on a jitter crash. The jitter runs before code is executed. That makes it likely that you are looking at the wrong code for the problem. It isn't the Main() method that caused the crash, most likely it is the form that you create in your Main method. Albeit that this is a guess, you forgot to post the code in your Main() method. To be on the safe side, a good practice is to separate the event registration from the code with the cooties, best done by suppressing inlining. Modify the Program.cs source code and make it look similar to this:

using System.Runtime.CompilerServices;
...
        [STAThread]
        static void Main() {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            RealMain();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static void RealMain() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Your code here
            //...
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
            var ex = (Exception)e.ExceptionObject;
            MessageBox.Show(ex.ToString(), "Unexpected error");
            Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
        }

You are likely to get the name back of one of your own methods. Do however not discount the possibility that you'll see a framework method. There are four distinct versions of .NET 4 and you cannot guess which one is on the user's machine. If that's the case then you made a mistake when you created the project, you added assembly references from the c:\windows\microsoft.net subdirectory instead of c:\program files\reference assemblies. Which will let you use a method that isn't available in an early version of .NET 4.

OTHER TIPS

MissingMethodException usually points to mismatches in the versions of your assemblies.

An example would be that you have an application that uses a DLL. Both are written by you. You now add a new method to a class in the DLL and use that method in the application.

Now, upon deployment, you only copy the new application but forget to copy the new DLL. The result will be a MissingMethodException when the application tries to use the new method.

Make sure all assemblies have the correct version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top