Last active
January 31, 2022 23:43
-
-
Save teyc/8659aabcd6d6b9d17da4285ec9cbe957 to your computer and use it in GitHub Desktop.
AspNetCore running as Windows Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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