Skip to content

With UseWindowsService, exceptions during configuration aren't reported as service startup exceptions #50018

Open

Description

@davidmatson

Describe the bug

Exceptions starting up the host aren't reported as Windows service startup failures.

To Reproduce

  1. Use the program below.
  2. Start the service.

Expected behavior

Net start reports the service failed to start, and the startup exception is logged in event viewer.

Actual behavior

Net start reports the service didn't respond to the control request in a timely fashion, and the startup exception is not logged in event viewer.

Additional context

Program.cs:

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;

static class Program
{
    static void Main()
    {
        new HostBuilder()
            .ConfigureLogging(l => l.AddConsole())
            .ConfigureServices((s) =>
            {
                throw new InvalidOperationException("Configuring services failed for some reason.");
            })
            .UseWindowsService()
            .Build()
            .Run();
    }
}

Activity

  1. analogrelay commented on Jan 8, 2020

    @analogrelay
    Contributor

    Seems like we should take a look at this. Hopefully there's a hook we can catch here. If it happens during process main we may be a little stuck since the exception is happening before any of the logging can be set up.

  2. simongymer commented on Mar 3, 2020

    @simongymer

    Just found this in a .NET core service I've written where there was a simple mistake in a connection string inside the appsettings.json file. It was throwing an exception, I was catching it and trying to log an error, but it never got logged and nothing useful in the event log either, so took me a while to debug.

  3. analogrelay commented on Apr 24, 2020

    @analogrelay
    Contributor

    Net start reports the service didn't respond to the control request in a timely fashion

    This is probably due to dotnet/extensions#2836 rather than the exception itself.

    the startup exception is not logged in event viewer.

    This is trickier since we don't have any logging set up yet. I'm surprised that the exception isn't going anywhere, since the runtime is supposed to write startup exceptions to the Application Event Log.

    Triage: We'll do a quick investigation after fixing dotnet/extensions#2836 and see if the event shows up somewhere now that we're properly exiting.

  4. transferred this issue fromdotnet/extensionson Mar 22, 2021
  5. ghost added
    untriaged
    on Mar 22, 2021
  6. removed
    untriaged
    on Mar 26, 2021
  7. added this to the Future milestone on Mar 26, 2021
  8. added
    backlog-cleanup-candidate
    on Sep 25, 2025
  9. davidmatson commented on Sep 25, 2025

    @davidmatson
    Author

    /keep-alive

  10. removed
    backlog-cleanup-candidate
    on Sep 25, 2025
  11. simongymer commented on Sep 25, 2025

    @simongymer

    +1

  12. rosebyte commented on Jan 20, 2026

    @rosebyte
    Member

    Duplicate of #50019?

  13. cincuranet commented on Jan 20, 2026

    @cincuranet
    Contributor

    @rosebyte I think the root cause might be same/similar, but it is slightly different problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions