如何检查程序集是否是使用调试或发布配置构建的?
我正在开始部署我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
检查此。这个想法是,您使用
Assembly.GetCustomAttributes()
获取程序集属性列表,并搜索DebuggableAttribute
,然后查找该属性是否具有IsJITTrackingEnabled
属性集。Check this. The idea is that you get the list of assembly attributes using
Assembly.GetCustomAttributes()
and search forDebuggableAttribute
and then find if such attribute hasIsJITTrackingEnabled
property set.我喜欢David 建议,但您也可以采用这种方式(
AssemblyInfo.cs
):这更加人性化,因为任何人都可以右键单击该程序集,以选择
Properties< /code> 并转到
详细信息
选项卡。I loved that David suggestion, but you could also go this way (
AssemblyInfo.cs
):This is more human friendly, as anyone can right-click that assembly, to select
Properties
and go toDetails
tab.如果是您的程序集,我相信使用 AssemblyConfiguration 属性是最好的方法。它被记录为“指定程序集的构建配置,例如零售或调试”。
根据您的构建配置,您可能有这样的代码:
然后检查程序集属性:(
我知道 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:
Then check the assembly attribute:
(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)
如果安装了 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.
假设只有 Debug 和 Release 配置,DEBUG 符号默认是使用 Debug 配置定义的,因此下面的代码位于 AssemblyInfo.cs(在 Properties 文件夹下)。
我使用 AssemblyTitle 而不是 AssemblyDescription,因为它将显示在我的 Windows 7 文件资源管理器属性中:
对于那些喜欢 David 和 stevieg 答案的人,这里有一个用 C# 编写的 LINQPad 脚本。要使用该脚本,您需要下载 LINQPad 5 并确保选择 C# 程序,如图所示在下面的屏幕截图中。
只需将 DLL_FOLDER_PATH 替换为指向包含要检查的 DLL 的文件夹即可。
可以在此处下载 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).
I use AssemblyTitle over AssemblyDescription as it will show up on my Windows 7 file explorer 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.
LINQPAD 5 can be downloaded here.
不要通过 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:
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.