WPF处理丢失的dll [英] WPF handling missing dlls

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

问题描述

我注意到,在某些情况下,我不分发应用程序所需的特定dll,该应用程序不提供任何错误,只是不加载(显示在屏幕上)。

I have noticed that in some situations, where I do not distribute a particular dll, which is required by application, the application doesn't not provide any errors, it just does not load (show on screen). It behaves like you haven't started it.

有人知道为什么会这样吗?

Does anyone know why is this happening?

编辑:重现步骤:


  1. 使用WPF应用程序项目和类库项目(ReferenceDll)创建解决方案。

  2. 在WPF应用程序项目中添加对类库项目的引用。

  3. 在类库中,将此方法添加到Class1

  1. Create a solution with WPF app project and class library project (ReferenceDll).
  2. Add reference in WPF app project to class library project.
  3. In class library, add this method to Class1

public int Calculate(int a,int b)
{
返回a + b;
}

public int Calculate(int a, int b) { return a + b; }

将此代码放入App.OnStartup:

place this code in App.OnStartup:

try
{
    int result = new ReferenceDll.Class1().Calculate(2, 4);
}
catch (Exception ex)
{
    File.WriteAllText(@"D:\Test\error.txt", ex.ToString());
}


  • 构建解决方案,然后从bin文件夹中删除ReferenceDll。运行应用程序。

  • build the solution then remove the ReferenceDll from the bin folder. Run the app.

    将不会创建文件,也不会显示应用程序。即使您在主视图中移动了Try catch代码,在某些按钮单击事件中,Catch {}也不会被触发,并且将显示非信息性消息

    No file will be created, app will not show. Even if you move Try catch code in the main view, in some button click event, Catch{} will never be fired and it will display non-informative message

    AppName has stopped working.
    

    并提供调试选项,这对最终用户没有用。

    and offer options to debug, which is of no use to end-user.

    推荐答案

    当在辅助线程上引发异常时,可能会发生这种情况。请参阅此页面上的备注部分

    That may happen when the exception is thrown on a secondary thread. See remarks section on this page:


    独立的或浏览器托管的WPF应用程序使用Application类检测未处理的异常(请参阅DispatcherUnhandledException)。但是,Application只能检测在Application类正在运行的同一线程上引发的未处理异常。通常,一个应用程序将具有一个主用户界面(UI)线程,因此Application类的未处理异常检测行为就足够了。但是,主UI线程上的Application类不会自动检测到在辅助线程上引发的未处理异常。

    Standalone or browser-hosted WPF applications use the Application class to detect unhandled exceptions (see DispatcherUnhandledException). However, Application can only detect unhandled exceptions that are thrown on the same thread that the Application class is running. Usually, an application will have one main user interface (UI) thread, so the unhandled exception detection behavior of the Application class is adequate. However, unhandled exceptions that are thrown on secondary threads are not automatically detected by the Application class on the main UI thread.

    您可以尝试使用此事件 catch 检测异常并记录错误:
    AppDomain.UnhandledException事件

    You can try using this event to catch detect the exception and log the error: AppDomain.UnhandledException Event

    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
    
    static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
       Exception e = (Exception) args.ExceptionObject;
       Console.WriteLine("MyHandler caught : " + e.Message);
    }
    



    更新:



    除了线程问题之外,如果将try ... catch块放到错误的位置,也会导致此问题。考虑以下示例:

    UPDATE:

    Apart from the threading issue it also can the cause if you put your try...catch block to the wrong place. Consider this example:

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
    
            Do();
    
        }
    
        private void Do()
        {
            try
            {
                int result = new ClassLibrary1.Class1().Calculate(2, 4);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("MyHandler caught by try...catch: " + ex.Message);
            }
        }
    }
    

    这将导致异常在调用Do()的行中,因为此处的CLR试图在此时解析程序集。

    This will result in an exception at the line where the Do() is called since the CLR here tries to resolve the assembly at this point. The exception is not caught and the app terminates.

    但是,如果您尝试执行以下操作:

    But if you try this one:

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
    
            try
            {
                Do();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("MyHandler caught by try...catch: " + ex.Message);
            }
        }
    
        private void Do()
        {
            int result = new ClassLibrary1.Class1().Calculate(2, 4);
        }
    }
    

    输出为:


    MyHandler被try ... catch捕获:无法加载文件或程序集'ClassLibrary1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或以下之一它的依赖关系。系统找不到指定的文件。

    MyHandler caught by try...catch: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    请注意,在同一函数中订阅该文件时,不会触发UnhandledException事件。引用了程序集。

    Note that the UnhandledException event is not fired when you subscribe to it in the same function where the assembly is referenced. This works as well:

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
    
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(
                (sender, args) =>
                {
                    Exception ex = (Exception)args.ExceptionObject;
                    Console.WriteLine("MyHandler caught by UnhandledException handler: " + ex.Message);
                });
    
            Do();
        }
    
        private void Do()
        {
            int result = new ClassLibrary1.Class1().Calculate(2, 4);
        }
    

    结果:


    由UnhandledException处理程序捕获的MyHandler:无法加载文件或程序集'ClassLibrary1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一。系统找不到指定的文件。

    MyHandler caught by UnhandledException handler: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    希望有帮助。

    这篇关于WPF处理丢失的dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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