Skip to content

Instantly share code, notes, and snippets.

@teyc
Last active January 31, 2022 23:43
Show Gist options
  • Save teyc/8659aabcd6d6b9d17da4285ec9cbe957 to your computer and use it in GitHub Desktop.
Save teyc/8659aabcd6d6b9d17da4285ec9cbe957 to your computer and use it in GitHub Desktop.
AspNetCore running as Windows Service
dotnet build
$binPath=(Get-Item bin\Debug\net6.0\*.exe).FullName
# Installs service
sc.exe create EML.AspNetCoreWindowsService binPath=$binPath DisplayName="Demo AspNetCore Windows Service"
# Starts service, hits the URL, stop-service
start-service EML.AspNetCoreWindowsService; curl.exe http://localhost:5000/; stop-service EML.AspNetCoreWindowsService
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting.WindowsServices;
var host = WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.Build();
// This call will block until the service is stopped.
host.RunAsService();
public class Startup: IStartup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
return services.BuildServiceProvider(validateScopes: true);
}
public void Configure(IApplicationBuilder app)
{
app.Run(ctx => ctx.Response.WriteAsync("Windows Service says hello"));
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="6.0.1" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment