【问题标题】:Find appSettings usages in a dll or exe在 dll 或 exe 中查找 appSettings 用法
【发布时间】:2014-06-13 20:34:48
【问题描述】:

我希望对遗留的 ASP.Net Web 应用程序进行一些清理,而 web.config 文件中混杂着大量看似未使用的 appSettings。不幸的是,许多引用的第三方类库都依赖于这些配置值中的一些,因此任何清理都带有应用程序将在某处失败的风险。

有没有办法通过一组 .dll 文件来“探索”以确定它们可能引用哪些 appSettings?我将使用它来确定缺少哪些键以及未引用哪些键。

-西格德

【问题讨论】:

    标签: .net appsettings


    【解决方案1】:

    您可以使用 Mono.Cecil,它可以更轻松地通过程序集中的 IL 代码。有了它,您可以找到所有对ConfigurationManager.AppSettings 的调用,然后将它们添加到列表或其他内容中。

    最后检查该列表并检查是否只使用了字符串(因此没有库将配置管理器包装在另一个类中)。

    可以在这里找到塞西尔:http://www.mono-project.com/Cecil

    工作示例:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using Mono.Cecil;
    using Mono.Cecil.Cil;
    
    namespace ConsoleApplication20
    {
        internal class Program
        {
            public const string SomOtherKey = "hey";
    
            public static void Main(string[] args)
            {
                var key1 = "max1";
                var key2 = "max2";
                var key3 = "max3";
                var key4 = "max4";
                var key5 = "max5";
                Console.WriteLine(ConfigurationManager.AppSettings["hello"]);
                Console.WriteLine(ConfigurationManager.AppSettings[key2]);
                Console.WriteLine(ConfigurationManager.AppSettings[key4]);
                Console.WriteLine(ConfigurationManager.AppSettings[key1]);
                Console.WriteLine(ConfigurationManager.AppSettings[key3]);
                Console.WriteLine(ConfigurationManager.AppSettings[key5]);
                Console.WriteLine(ConfigurationManager.AppSettings[SomOtherKey]);
    
                var dlls = Directory.GetFiles(Environment.CurrentDirectory, @"*.exe");
                foreach (var dll in dlls)
                {
                    var module = ModuleDefinition.ReadModule(dll);
    
                    foreach (var type in module.Types)
                    {
                        foreach (var method in type.Methods)
                        {
                            FindConfigurationManager(method);
                        }
                    }
                }
    
                Console.ReadLine();
            }
    
    
            public static void FindConfigurationManager(MethodDefinition method)
            {
                for (var i = 0; i < method.Body.Instructions.Count; i++)
                {
                    var instruction = method.Body.Instructions[i];
                    if (instruction.OpCode == OpCodes.Call)
                    {
                        var methodCall = instruction.Operand as MethodReference;
                        if (methodCall != null && methodCall.Name == "get_AppSettings")
                        {
                            var nextInstruction = method.Body.Instructions[i + 1];
                            var variable = "";
    
                            if (nextInstruction.OpCode == OpCodes.Ldloc_0)
                                variable = FindString(method.Body.Instructions, 0);
                            else if (nextInstruction.OpCode == OpCodes.Ldloc_1)
                                variable = FindString(method.Body.Instructions, 1);
                            else if (nextInstruction.OpCode == OpCodes.Ldloc_2)
                                variable = FindString(method.Body.Instructions, 2);
                            else if (nextInstruction.OpCode == OpCodes.Ldloc_3)
                                variable = FindString(method.Body.Instructions, 3);
                            else if (nextInstruction.OpCode == OpCodes.Ldloc_S)
                                variable = FindString(method.Body.Instructions, ((VariableReference) nextInstruction.Operand).Index);
                            else
                                variable = nextInstruction.Operand.ToString();
    
                            // next argument is a string value
                            Console.WriteLine("\t" + variable);
                        }
                    }
                }
            }
    
            private static string FindString(IEnumerable<Instruction> instructions, int index)
            {
                var current = -1;
                foreach (var instruction in instructions)
                {
                    if (instruction.OpCode != OpCodes.Ldstr)
                        continue;
    
                    current++;
                    if (current == index)
                        return instruction.Operand.ToString();
                }
    
                return "not found";
            }
    
    
        }
    }
    

    输出:

    免责声明:我以前没有做过这样的事情。示例解决方案可能不是最好的,但它确实适用于某些情况:) 您可能需要扩展它。

    工作原理

    我在回答这个问题时才发现这一点,所以我写的可能不是 100% 准确。 如果在配置管理器中访问密钥,有两种不同的方式。

    第一个是硬编码字符串。在这种情况下,这很容易,因为ldstr(加载字符串)指令直接用作方法引用之后的下一条指令。这就是为什么我们可以通过nextInstruction.Operand.ToString(); 来加载它。

    另一种方法是使用变量加载键名。在这种情况下,它有点棘手,因为有几条指令可用于加载变量。因此,我们需要所有这些 if 来获取字符串值。我所做的是遍历整个方法指令集并简单地计算ldstr 指令的数量,直到我得到方法调用中使用的相同索引。

    请注意,我不确定第二种选择是否适用于所有情况。

    【讨论】:

      【解决方案2】:

      尝试使用 ILSpy,反映到 DLL 中并搜索值。不过这会很耗时

      【讨论】:

      • 我担心这只能解决我的问题的一半。理想情况下,我想获取那些实际从 dll 引用的 appSettings 键的列表。由于 web config 只在应用程序根目录中声明 appSettings,并且 web.config 是可读的,我可以比较两者以确定哪些未使用,哪些丢失。不过,感谢您的提示!
      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 2017-12-08
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      相关资源
      最近更新 更多