简体   繁体   English

#if DEBUG无法在Windows Service版本中运行

[英]#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? 有什么建议么?

Ensure that the "DEBUG" constant is not defined in Release mode. 确保在发布模式下未定义“ DEBUG”常量。

在此处输入图片说明

Also, verify that your project is set to build in Release mode when the solution is in Release mode. 此外,当解决方案处于“发布”模式时,请验证您的项目是否设置为“发布”模式。

在此处输入图片说明

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.

相关问题 我应该使用调试构建可执行文件还是发布构建可执行文件来安装Windows服务? - Should I use debug build executable or release build executable to install a windows service? Windows Service在调试中执行代码,但在发布中不执行 - Windows Service executes code in debug, but not in release C#Windows Service安装在调试版本上而不是发布版本上 - C# windows service installs on debug not on release .Net Release构建比Debug慢 - .Net Release build working slower than Debug Windows Phone 8应用程序IsolatedStorageException / SecurityException on release built,NOT debug build - Windows Phone 8 app IsolatedStorageException / SecurityException on release built, NOT debug build GPG加密在控制台调试模式下工作,但在发布模式下不工作(窗口服务) - GPG encryption is working in console debug mode but not in release mode( window service) 发布版本中的Debug.WriteLine - Debug.WriteLine in release build monodevelop-工作调试但不工作发布 - monodevelop - working debug but not working release 在发布版本中调用#if DEBUG和Conditional(“ DEBUG”) - #if DEBUG and Conditional(“DEBUG”) called in Release build C#Windows服务-WebBrowser控件在发布模式下不起作用 - C# Windows Service - WebBrowser Control not working in Release Mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM