如何检查程序集是否是使用调试或发布配置构建的?

发布于 2024-08-19 16:20:26 字数 98 浏览 15 评论 0原文

我正在开始部署我的 Web 应用程序,我需要保证所有要部署的程序集都是使用发布配置构建的。我们的系统是使用C#/.Net 3.5开发的。

有什么办法可以实现这一点吗?

I'm starting deployment of my web application and I need to guarantee that all the assemblies that are going to be deployed were built using Release configuration. Our system was developed using C#/.Net 3.5.

Is there any way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流星番茄 2024-08-26 16:20:26

检查。这个想法是,您使用 Assembly.GetCustomAttributes() 获取程序集属性列表,并搜索 DebuggableAttribute,然后查找该属性是否具有 IsJITTrackingEnabled属性集。

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

Check this. The idea is that you get the list of assembly attributes using Assembly.GetCustomAttributes() and search for DebuggableAttribute and then find if such attribute has IsJITTrackingEnabled property set.

    public bool IsAssemblyDebugBuild(Assembly assembly)
    {
        return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled);
    }
做个ˇ局外人 2024-08-26 16:20:26

我喜欢David 建议,但您也可以采用这种方式(AssemblyInfo.cs):

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

这更加人性化,因为任何人都可以右键单击该程序集,以选择Properties< /code> 并转到 详细信息 选项卡。

I loved that David suggestion, but you could also go this way (AssemblyInfo.cs):

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

This is more human friendly, as anyone can right-click that assembly, to select Properties and go to Details tab.

窝囊感情。 2024-08-26 16:20:26

如果是您的程序集,我相信使用 AssemblyConfiguration 属性是最好的方法。它被记录为“指定程序集的构建配置,例如零售或调试”。

根据您的构建配置,您可能有这样的代码:

#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;
}

我知道 Rubens Farias 的 R. Schreurs 评论也说了同样的话,但在看到评论之前我已经在其他地方找到了此信息,所以我相信这需要更重要的条目,例如完整回复而不是评论)

If it is your assembly I believe using the AssemblyConfiguration attribute is the best approach. It is documented as "Specifies the build configuration, such as retail or debug, for an assembly."

Depending on your build configurations you might have code like this:

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

Then check the assembly attribute:

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;
}

(I know R. Schreurs comment at Rubens Farias says the same, but I've find this information somewhere else before seeing the comment so I believe this requires a more important entry like a full response instead of a comment)

妄司 2024-08-26 16:20:26

如果安装了 Reflector,您还可以单击程序集并在“反汇编程序”窗格中查找可调试属性 ([程序集:Debuggable()])。

If you have Reflector installed you can also click on the assembly and look for the debuggable attribute ([assembly: Debuggable()]) in the Disassembler pane.

丢了幸福的猪 2024-08-26 16:20:26

假设只有 Debug 和 Release 配置,DEBUG 符号默认是使用 Debug 配置定义的,因此下面的代码位于 AssemblyInfo.cs(在 Properties 文件夹下)。

#if DEBUG
[assembly: AssemblyTitle("Debug")]
#else
[assembly: AssemblyTitle("Release")]
#endif

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

DLL 文件属性

对于那些喜欢 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;
        }
    }
}

检查版本或使用 LINQPad5 进行调试构建

可以在此处下载 LINQPAD 5。

Assuming only Debug and Release configuration, DEBUG symbol is by default defined with Debug configuration, so the code below in AssemblyInfo.cs (under Properties folder).

#if DEBUG
[assembly: AssemblyTitle("Debug")]
#else
[assembly: AssemblyTitle("Release")]
#endif

I use AssemblyTitle over AssemblyDescription as it will show up on my Windows 7 file explorer properties:

DLL File properties

For those who like David and stevieg's answer, here is a LINQPad script written in C#. To use the script, you need to download LINQPad 5 and make sure C# Program is selected as shown in screenshot below.

Simply replace DLL_FOLDER_PATH to point to folder containing the DLLs to be inspected.

// 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 can be downloaded here.

疾风者 2024-08-26 16:20:26

不要通过 Visual Studio 部署到生产环境。查看持续集成和脚本化构建(例如使用NAnt,或者可能是更清晰的内容,例如 假的)。

F5 键不是构建过程

对于那些认为这不能回答问题的批评者,OP 写道:

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

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

Don't deploy to production via Visual Studio. Look into Continuous Integration and scripted builds (such as with NAnt, or perhaps something more legible like FAKE).

The F5 Key Is Not a Build Process

To detractors who believe that this does not answer the question, the OP wrote:

...I need to guarantee that all the assemblies that are going to be
deployed were built using Release configuration.

To guarantee that, use a build server such as TeamCity and possibly a release management tool like Octopus Deploy. Lock down your production systems so that developers must go through the official build process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文