[英]#if DEBUG not working on a Windows Service release build
In the constructor of my Windows Service I have some code within a #if DEBUG condition: 在我的Windows服务的构造函数中,我在#if DEBUG条件下有一些代码:
#if DEBUG
var container = CompositionRoot.DiBootstrapper.Initialize();
_initializeApplication = container.Resolve<IInitializeApplication>();
_initializeApplication.Initialize();
#endif
When I compile/build the the project in release mode and install it as a Windows Service, the code within the #if DEBUG condition is executed. 当我以发布模式编译/构建项目并将其作为Windows服务安装时,将执行#if DEBUG条件内的代码。
I would only like the code to run when I debug the application. 我只希望在调试应用程序时运行代码。
Any suggestions? 有什么建议么?
You can use 您可以使用
if (System.Diagnostics.Debugger.IsAttached)
{
// ....
}
instead of the compile time constant to achive this. 而不是达到这个目标的编译时间常数。 The code will allways be compiled into your code but only executed if you are debugging your application.
该代码将始终编译为您的代码,但仅在调试应用程序时才执行。 Please be aware that this will also be executed if someone else attaches a debugger to your app.
请注意,如果其他人将调试器附加到您的应用程序,此操作也将执行。
However: Most likely you have configured something wrong. 但是:很可能是您配置错误。
Inside Visual Studio, go to "BUILD -> Configuration Manager" and verify that the specific project is set to "DEBUG". 在Visual Studio中,转到“构建->配置管理器”,并验证特定项目是否设置为“调试”。
If that is the case and your project has more than one assembly: verify that your release build of your main exe uses your newly generated Release Build for assembly references and not an old reference which is referenced from inside the bin/debug
folder. 如果是这种情况,并且您的项目具有多个程序集,请执行以下操作:验证主exe的发行版本使用新生成的发行版本作为程序集引用,而不是从
bin/debug
文件夹内部引用的旧引用。
It looks you use IOC so maybe your main assembly A
has no direct reference to the assembly which contains the #DEBUG
constant B
. 看来您使用的是IOC,所以也许您的主装配件
A
没有直接引用包含#DEBUG
常量B
的#DEBUG
。 So after a change to B
if you build A
the project B
is not rebuild. 因此,在更改为
B
如果您生成A
,则不会重建项目B
You can solve this by right clicking your solution and select Build dependencies
, in the drop down box select A
and check B
您可以通过右键单击解决方案并选择“
Build dependencies
来解决此问题,在下拉框中选择A
并选中B
您的服务仍在调试模式下构建(请检查配置管理器),或者在发布模式中指定了条件编译符号(请检查项目属性)。
Check the project configuration, then select the Release . 检查项目配置,然后选择发布 。 Try to uncheck the Define DEBUG constant .
尝试取消选中Define DEBUG常量 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.