ASP.NET Core 6 Hosting as a Windows Service

477 views Asked by At

I'm currently facing issues while hosting ASP.NET Core 6 as a Windows service. I have tried hosting a sample program and it works fine (code pasted below). However, if I try to change the endpoints or use the kestrel server using a config file to change endpoints, I'm not able to host it as a Windows service. Has anyone faced a similar challenge?

My current working sample looks like the below program

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

var builder = WebApplication.CreateBuilder(options);

builder.Services.AddRazorPages();
builder.Services.AddHostedService<Services.Worker>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

builder.Host.UseWindowsService();

var app = builder.Build();

app.UseRouting();
app.MapControllers();
app.Run();

The worker service looks like this:

public class Worker : BackgroundService
{
    public Worker(ILoggerFactory loggerFactory)
    {
        Logger = loggerFactory.CreateLogger<Worker>();
    }

    public ILogger Logger { get; }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        Logger.LogInformation("Worker is starting.");

        stoppingToken.Register(() => Logger.LogInformation("Worker is stopping."));

        while (!stoppingToken.IsCancellationRequested)
        {
            Logger.LogInformation("Worker is doing background work.");

            await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
        }

        Logger.LogInformation("Worker has stopped.");
    }
}

However, if I try to change the endpoints in the main program using the following code I'm not able to host it as a Windows service.

app.Urls.Add("http://localhost:5020");
app.Urls.Add("https://localhost:5021");

My goal is the host it as a Windows service and authenticate using certificates. Also, I found a blogpost that works well with .NET 4.7:

https://rahulsahay19.medium.com/publishing-asp-net-core-as-windows-service-852fc2b930b8

Has anyone tried to authenticate using certificates, configure URLs and host an ASP.NET Core 6 program as a Windows service?

1

There are 1 answers

0
Vishwanath Heddoori On

I was able to solve this issue by generating a certificate myself and using it in the config file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Kestrel": {
    "Endpoints": {
      "Localhost": {
        "Url": "https://localhost:5556",
        "ClientCertificateMode": "AllowCertificate",
        "Certificate": {
          "Path": "Certificate.pfx",
          "Password": "Password"
        }
      },
      "AnyIP": {
        "Url": "https://0.0.0.0:5557",
        "ClientCertificateMode": "AllowCertificate",
        "Certificate": {
          "Path": "Certificate.pfx",,
          "Password": "Password"
        }
      }
    }
  }
}