Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

452
Views
ASP.NET Core .NET 6 Preview 7 Windows Service

I created a new ASP.NET Core project with Visual Studio 2022 Preview and I am trying to run it as a Windows Service. I downloaded the latest Microsoft.Extensions.Hosting.WindowsServices package (6.0.0-preview.7.21377.19).

When researching online the function .UseWindowsService() goes into CreateHostBuilder method. But in the new template it looks different. I cannot understand where I should call .UseWindowsService in the new template. This is my current code, it looks like the service is starting but then when I browse to localhost:5000 it gives me 404 error

using Microsoft.OpenApi.Models;
using Microsoft.Extensions.Hosting.WindowsServices;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseWindowsService(); // <--- Added this line

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "MyWindowsService", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
        app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyWindowsService v1"));
}

app.UseAuthorization();

app.MapControllers();


app.Run();

I published my service exe like this

dotnet publish -c Release -r win-x64 --self-contained
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The following coding sets the lifetime to WindowsServiceLifetime and enables logging to the event log. In most cases this should be all you need to run the app as a Windows Service.

if (WindowsServiceHelpers.IsWindowsService())
{
    appBuilder.Services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
    appBuilder.Logging.AddEventLog(settings =>
    {
        if (string.IsNullOrEmpty(settings.SourceName))
        {
            settings.SourceName = appBuilder.Environment.ApplicationName;
        }
    });
}
over 4 years ago · Santiago Trujillo Report

0

Since simply using

builder.Host.UseWindowsService();

will not work with WebApplication.CreateBuilder() (see), but instead will throw the exception

Exception Info: System.NotSupportedException: The content root changed from "C:\Windows\system32\" to "...". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.

or rather will cause this error

Start-Service : Service 'Service1 (Service1)' cannot be started due to the following error: Cannot start service Service1 on computer '.'.

when trying to start the Service with Start-Service in PowerShell, I found a workaround that worked for me

using Microsoft.Extensions.Hosting.WindowsServices;

var options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

builder.Host.UseWindowsService();

here: An asp.net core web api application, using "UseWindowsService", reports an error “System.NotSupportedException: The content root changed. Changing the host configuration is not supported ” when starting the service

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!