【问题标题】:Run Console App Despite FileNotFoundException尽管存在 FileNotFoundException,但运行控制台应用程序
【发布时间】:2014-12-04 10:36:24
【问题描述】:

我们的一些应用程序依赖于用于日志记录和配置的通用 dll。这个通用 dll 被添加到我们服务器上的 GAC 中,并且不包含在我们部署的 bin 文件夹中。

我想编写一个简单的控制台应用程序来检查 common.dll 是否已添加到服务器上的 GAC。 我没有运气使用 fusion 或 assembly.load 以编程方式检查这个:Check GAC for an assembly

我的想法是在本地包含对 dll 的引用,确保关闭本地复制,然后在调用 dll 中的方法时尝试在服务器上捕获 FileNotFoundException。

类似的东西

  static void Main()
        {
            try
            {                 
                Common.Logger.LogInfo("Testing logger."); //call to the dll
                Console.WriteLine("loaded successfully");
                Console.ReadLine();
            }
            catch (System.IO.FileNotFoundException e)
            {
                Console.WriteLine("Common dll missing!" + e.Message);
                Console.ReadLine();
            }
        }  

但由于 dll 不存在,应用程序将崩溃并在调用 main 方法之前抛出 FileNotFoundException。我假设在运行 .exe 时有一些初始检查是否存在所有 dll - 有没有办法禁用它?

【问题讨论】:

    标签: c# .net dll


    【解决方案1】:

    尝试将其移至单独的方法中:

    static void Main()
    {
        try
        {     
            CheckLogger();            
            Console.WriteLine("loaded successfully");
            Console.ReadLine();
        }
        catch (System.IO.FileNotFoundException e)
        {
            Console.WriteLine("Common dll missing!" + e.Message);
            Console.ReadLine();
        }
    }  
    
    static void CheckLogger()
    {
        Common.Logger.LogInfo("Testing logger."); //call to the dll
    }
    

    您需要Main 方法开始运行 以进入try/catch 块,如果它不能(JIT)编译时间>。但是由于每种方法都是单独编译的,因此应该可以。缺少程序集的 FileNotFoundException 在编译直接依赖于程序集的方法期间被触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多