如何检查程序集是使用Debug还是Release配置构建的?

53

我要开始部署我的Web应用程序,并且需要确保所有将要部署的组件都是使用发布配置构建的。我们的系统是使用C#/.Net 3.5开发的。

有没有办法实现这个要求?

6个回答

50

看看这个链接。其思路是使用Assembly.GetCustomAttributes()获取程序集属性列表,查找DebuggableAttribute,然后找到是否设置了IsJITTrackingEnabled属性。

    public bool IsAssemblyDebugBuild(Assembly assembly)
    {
        return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled);
    }

15
讽刺并不是必要的。在互联网搜索中,最困难的部分通常是知道该问些什么。 - doogle
阅读文章,但是去评论区寻找更好的解决方案:var result = assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Select(da => da.IsJITTrackingEnabled).FirstOrDefault(); - stevieg

30

我喜欢那个David的建议,但你也可以用以下方式 (AssemblyInfo.cs):

#if DEBUG
[assembly: AssemblyDescription("Your application assembly (DEBUG version)")]
#else if RELEASE
[assembly: AssemblyDescription("Your application assembly (RELEASE version)")]
#endif

这样更加人性化,因为任何人都可以右键单击该程序集,选择 属性 并进入 详细信息 选项卡。


2
+1:这实际上是这个汇编指令的目的。 - Mike Post
8
我建议使用AssemblyConfiguration而不是AssemblyDescription。AssemblyConfiguration被记录为“指定程序集的构建配置,例如零售版或调试版”。 - R. Schreurs
这个可以工作,但是到处都要加 #IF 来检测代码并不是很好。 - ps2goat
@R.Schreurs,就我所见(在Windows 7上),AssemblyConfiguration 在 DLL 属性或 ildasm 中并未显示。Windows 10 可能会显示其他信息,但我不确定。 - NibblyPig

11
如果这是你的程序集,我相信使用 AssemblyConfiguration 属性是最好的方法。它的文档说明为“指定程序集的构建配置,例如 retail 或 debug。”
根据你的构建配置,你可能会有类似于以下代码的内容:
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

然后检查装配属性:

public static bool IsAssemblyConfiguration(Assembly assembly, string configuration)
{
    var attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
    if (attributes.Length == 1)
    {
        var assemblyConfiguration = attributes[0] as AssemblyConfigurationAttribute;
        if (assemblyConfiguration != null)
        {
            return assemblyConfiguration.Configuration.Equals(configuration, StringComparison.InvariantCultureIgnoreCase);
        }
    }
    return true;
}

我知道R.Schreurs在Rubens Farias的评论中也有相同的说法,但在看到该评论之前,我已经在其他地方找到了这些信息,因此我认为这需要一个更重要的条目,如全面回答,而不是只是一个评论。


我已经设置了这个标志,但是在 DLL 属性或使用 ildasm 中看不到它。 - NibblyPig
它没有声明它会执行这个操作。也许你应该遵循另一个回答的建议,它说可以这样做。 - Guillermo Ruffino

2
假设只有 Debug 和 Release 两种配置,DEBUG 符号默认在 Debug 配置中被定义,因此在 AssemblyInfo.cs 文件(位于 Properties 文件夹下)中的以下代码:
#if DEBUG
[assembly: AssemblyTitle("Debug")]
#else
[assembly: AssemblyTitle("Release")]
#endif

我使用AssemblyTitle而不是AssemblyDescription,因为它将显示在我的Windows 7文件资源管理器属性中:

DLL File properties

对于喜欢David和stevieg的回答的人,这里是用C#编写的一个LINQPad脚本。要使用该脚本,您需要下载 LINQPad 5并确保选择C#程序,如下面的屏幕截图所示。
只需将DLL_FOLDER_PATH替换为指向包含要检查的DLL的文件夹即可。
// TODO - Specify your folder containing DLLs to inspect
static string DLL_FOLDER_PATH = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0";
void Main()
{
    (from dllPath in Directory.GetFiles(DLL_FOLDER_PATH, "*.dll")
    let assembly = dllPath.SafeLoad()
    let build = assembly == null ? "Error" : (dllPath.SafeLoad().IsAssemblyDebugBuild() ? "Debug" : "Release")
    select new {
        Assembly_Path = dllPath,
        Build = build,
    }).Dump();
}
static class Extensions {
    public static bool IsAssemblyDebugBuild(this Assembly assembly)
    {
        return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Select(da => da.IsJITTrackingEnabled).FirstOrDefault();
    }
    public static Assembly SafeLoad(this string path){
        try{
            return Assembly.LoadFrom(path);
        }
        catch {
            return null;
        }
    }
}

Checking release or debug build using LINQPad5

LINQPAD 5可以在这里下载。

2
如果您已经安装了Reflector,您也可以单击程序集并在反汇编窗格中查找debuggable属性([assembly: Debuggable()])。

0
不要通过Visual Studio部署到生产环境。研究持续集成和脚本构建(例如使用NAnt,或者可能更易读的FAKE)。 F5键不是构建过程 对于那些认为这并没有回答问题的批评者,OP写道:

...我需要保证所有将要部署的程序集都是使用发布配置构建的。

为了保证这一点,使用构建服务器,如TeamCity,可能还需要一个发布管理工具,如Octopus Deploy。锁定您的生产系统,以便开发人员必须经过官方的构建过程。

2
这完全没有回答问题。拥有一个构建过程是个好主意,但这不是一个答案。 - jasonmw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接