诊断MissingMethodException [英] Diagnosing MissingMethodException

查看:520
本文介绍了诊断MissingMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一台计算机上启动我的应用程序时,它立即退出,表明它已停止工作。在事件日志中,我看到MissingMethodException是原因。没有显示异常对话框,并且在事件日志详细信息中,我看不到任何详细的错误消息(例如,包含找不到方法:xyz)。



事件处理程序信息说(试图将其翻译成英文,可能不是逐字记录)


应用程序:Myapp.exe Framework版本v4.0.30319



说明:该进程由于未处理的异常而终止



异常信息:System.MissingMethodException



堆栈:在MyApp.MainClass.Main(System.String [])


当引发此异常时,如何找到缺少的哪个方法?它是一个托管应用程序,但具有许多本机依赖性。



编辑:
编译的程序集都是在同一版本中编译的,即在应用程序的托管代码中没有版本不匹配。

这是一个内置于VS 2012的Windows Forms应用程序,但是定位到4.0。在只有Framework 4.0的所有计算机上都不会引发该错误,因此这似乎不是问题。

解决方案

MissingMethodException 告诉您其Message属性缺少什么方法。但是,程序员常常忽略了给它一个告诉你的机会。您必须为AppDomain.UnhandledException事件编写事件处理程序,并显示或记录e.UnhandledException对象。



请注意,您的程序在抖动崩溃时很早就崩溃了。抖动在执行代码之前 运行。这很可能导致您正在寻找错误的代码来解决该问题。导致崩溃的原因不是Main()方法,很可能是您在Main方法中创建的表单。尽管这只是一个猜测,但您忘记将代码发布到Main()方法中。为了安全起见,一个好的做法是将事件注册与带有代码的代码分开,最好通过抑制内联来实现。修改Program.cs源代码,并使它看起来类似于:

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

[MethodImpl(MethodImplOptions.NoInlining)]
静态void RealMain(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//您的代码在这里
// ...
}

static void CurrentDomain_UnhandledException(object sender,UnhandledExceptionEventArgs e){
var ex =( e.ExceptionObject;
MessageBox.Show(ex.ToString(),意外错误);
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
}

您很可能会用自己的方法之一取回名称。但是,请不要忽视您会看到框架方法的可能性。 .NET 4有四个不同的版本,您无法猜测用户计算机上的哪个版本。如果是这种情况,那么您在创建项目时就犯了一个错误,您是从c:\windows\microsoft.net子目录中添加了程序集引用,而不是c:\program files\参考程序集。这样您就可以使用.NET 4早期版本中没有的方法。


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.

解决方案

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.

这篇关于诊断MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆