2

I am debugging a Windows service (by hitting F5 in Visual Studio 2010) using the following code:

In Program.cs file:

static void Main() {

    if (!Environment.UserInteractive) {
        // We are not in debug mode, startup as service

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyServer() };
        ServiceBase.Run(ServicesToRun);
    } 
    else {
        // We are in debug mode, startup as application

        MyServer service = new MyServer();
        service.StartService();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

And in MyServer.cs file:

public void StartService() {
    this.OnStart(new string[0]);
}

In the past, I used the Debug.WriteLine("xxx") line without any problem in all my service code, but now I noticed that all Debug.WriteLine() lines are not called anymore.

I clearly see that the debugger is jumping over these lines when I debug with Step Into (F11) - (thus, there is no output text on the output Window), whereas the service is correctly started in "debug mode".

I don't understand why the Debug code won't be called. Suggestions?

I build my solution in debug mode (Define DEBUG constant is checked), but I noticed that the code surrounded by #if DEBUG ... #endif is not called. That is weird...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Otiel
  • 18,404
  • 16
  • 78
  • 126
  • 1
    Sounds like you have the same problem as described here: http://stackoverflow.com/questions/4346271/debug-write-not-working – Alex Florescu Aug 16 '11 at 12:58
  • Are you not Compiling your application as Release? – Jethro Aug 16 '11 at 13:00
  • @Jethro No, as Debug. _"Define DEBUG constant"_ is checked. – Otiel Aug 16 '11 at 13:03
  • Your posted code does not have a single Debug.WriteLine please post the code with the actual problem not an example code that clearly does NOT. – Security Hound Aug 16 '11 at 13:18
  • 1
    @Ramhound I post the code that is useful to understand how I start my service... If you had read my question, you would have seen that I said _"all Debug.WriteLine() lines are not called"_, thus I don't see the point of pasting a code that has a `Debug.WriteLine`... But if you want it, I can paste you one: `Debug.WriteLine("Why downvote my question?!");` – Otiel Aug 16 '11 at 13:23
  • @anothem, no I already did what is proposed in the accepted answer. – Otiel Aug 16 '11 at 13:41

5 Answers5

6

Seems the Debug symbols are missing.

  1. Clean your solution, restart Visual Studio, rebuild the solution.
  2. Make sure the Configuration Manager is in Debug mode.
  3. Check in Tools > Options > Debugging and Uncheck "Enable Just My Code"

Happened to me and this seemed to help.

Michael D. Irizarry
  • 6,186
  • 5
  • 30
  • 35
  • My project was not in Debug mode in the Configuration Manager (but was in the solution properties). Thanks. – Otiel Aug 16 '11 at 13:50
  • 1
    Your answer saved my life. Especially steps #1 and #2 (#3 has already been unchecked). Even [this thread](http://stackoverflow.com/q/1159755/1088880) and [this one](http://stackoverflow.com/q/935180/1088880) didn't help. Thanks a million + upvote. – scatmoi Oct 19 '12 at 21:36
1

I had the same random problem.

I was able to just fix it by:

  1. Uncheck the Project Properties -> Build -> Define DEBUG constant
  2. Click Save
  3. Check 'Define DEBUG constant' option again
  4. Click Save
  5. Rebuild the Project
  6. Run

After doing this it hit the #if DEBUG line as expected.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brock Hensley
  • 3,617
  • 2
  • 29
  • 47
1

If you aren't using a debug build, the code is excluded by the compiler. The signature of the method has something similar to:

[Conditional("DEBUG")]
public static void WriteLine(string message) { }

That attribute tells the compiler to only include calls to the method in question, when you're doing debug builds. Therefore, if you aren't compiling with the DEBUG flag set (say, a release build) then the compiler drops the call altogether.

Same is true of Debug.Assert.

You can use this attribute in your own code too, with any label you like.

Another way of thinking about this attribute is that it forces all calls to the method having the attribute to be wrapped in directives -- something like:

    DoSomething();

#if DEBUG
    Debug.WriteLine("I'm a debug build");
#endif

    DoSomethingElse();

EDIT

What is it you are trying to achieve via Environment.UserInteractive? Perhaps you mean Debugger.IsAttached? If you're wanting to check if it's a debug build, then you can use the ConditionalAttribute as above, or #if #endif directives.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • By using `Environment.UserInteractive`, I just want to differentiate if the service is started via Visual Studio (so by me, hitting `F5`) or if it is started normally, when installed with other Windows services. – Otiel Aug 16 '11 at 13:14
  • See my edit, solution don't go through `#if DEBUG` even if `Define DEBUG constant` is checked. – Otiel Aug 16 '11 at 13:37
  • @Leito, that shouldn't be the case. Are you building from VS? What value is selected in the 'Solution Configurations' dropdown on the toolbar? – Drew Noakes Aug 17 '11 at 16:43
  • I have found my problem, thx. My project was not in Debug mode in the Configuration Manager. – Otiel Aug 18 '11 at 07:17
0

Environment.UserInteractive is not the same thing as Debug mode -- that just means that you are running it in the console instead of as a service. Make sure that your build configuration is set to Debug.

Jay
  • 56,361
  • 10
  • 99
  • 123
  • But it my case (running the service by hitting `F5`), it gives normally the same results, right? Build and debug configurations are already set to _Debug_. – Otiel Aug 16 '11 at 13:01
  • F5 runs the project with the current configuration, which could be Debug, Release, or any other configuration that you define. Your project settings have a checkbox for "Define DEBUG constant" -- this should also be checked. – Jay Aug 16 '11 at 13:06
0

Navigate to Project Properties -> Build and be sure that "Define DEBUG Constant" and "Define TRACE Constant" checkboxes are checked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131