Skip to content

UseWindowsService always reports successful service startup even when Host startup fails #50019

@davidmatson

Description

@davidmatson

Describe the bug

When using .UseWindowsService(), the windows service always reports success from startup, regardless of whether the host actually starts up successfully.
A clear and concise description of what the bug is.

To Reproduce

  1. Use the code below.
  2. Start the service with net start.

Expected behavior

Net start indicates the service failed to start.

Actual behavior

Net start indicates the service started successfully.

Additional context

Note that on some older versions of .NET Framework (pre 4.7.2 maybe), service startup exceptions aren't propagated correctly.

Program.cs:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

static class Program
{
    static void Main()
    {
        new HostBuilder()
            .ConfigureLogging(l => l.AddConsole())
            .ConfigureServices((s) =>
            {
                s.AddHostedService<TestService>();
            })
            .UseWindowsService()
            .Build()
            .Run();
    }
}

class TestService : IHostedService
{
    readonly ILogger log;

    public TestService(ILogger<TestService> log)
    {
        this.log = log;
    }


    public Task StartAsync(CancellationToken cancellationToken)
    {
        log.LogInformation("Starting...");
        throw new InvalidOperationException("Test when startup always fails.");
        //log.LogInformation("Started.");
        //return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        log.LogInformation("Stopping...");
        log.LogInformation("Stopped.");
        return Task.CompletedTask;
    }
}

TestHostBuilder.csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{9463B6C5-BC95-43AA-968F-651084C85DAE}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>TestHostBuilder</RootNamespace>
    <AssemblyName>TestHostBuilder</AssemblyName>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting">
      <Version>3.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices">
      <Version>3.1.0</Version>
    </PackageReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>```

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions