dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.24k stars 4.73k forks source link

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

Open davidmatson opened 4 years ago

davidmatson commented 4 years ago

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();
    }
}
analogrelay commented 4 years ago

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.

simongymer commented 4 years ago

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.

analogrelay commented 4 years ago

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

This is probably due to https://github.com/dotnet/extensions/issues/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.

dotnet-issue-labeler[bot] commented 3 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 3 years ago

Tagging subscribers to this area: @eerhardt, @maryamariyan See info in area-owners.md if you want to be subscribed.

Issue Details
### 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: ```c# 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(); } } ```
Author: davidmatson
Assignees: -
Labels: `area-Extensions-Hosting`, `untriaged`
Milestone: -