DEV Community

Cover image for How to Host ASP.NET Core 3.1 Web Applications as Windows Service

How to Host ASP.NET Core 3.1 Web Applications as Windows Service

Sumit Kharche on February 19, 2020

In this article, we will be discussing how to deploy & host ASP.NET Core 3.1 Web API as a Windows Service. You may have one question in mind li...
Collapse
 
dn32 profile image
Marcelo Vieira de Souza
        HostStatic = CreateHostBuilder(args).Build();
        HostStatic.Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        var root = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

        Console.WriteLine("Root: " + root);

        var builder = Host.CreateDefaultBuilder(args)
               .ConfigureLogging(logging =>
               {
                   logging
                   .ClearProviders()
                   .AddFilter("Microsoft", LogLevel.Error)
                   .AddFilter("System", LogLevel.Error);
               })
              .ConfigureWebHostDefaults(webBuilder =>
              {
                  webBuilder
                  .UseUrls(LocalHost)
                  .UseStartup<Startup>();
              });

        if (Utilitario.EhLinux())
            builder = builder.UseSystemd();
        else
            builder = builder.UseWindowsService();

        builder.ConfigureServices((hostContext, services) =>
          {
              services.AddHostedService<Worker>();
          });

        return builder;
    }
}


public class Worker : BackgroundService
{
    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        return Task.Run(() => { });
    }

    public override Task StopAsync(CancellationToken cancellationToken)
    {
        return base.StopAsync(cancellationToken);
    }
}
Collapse
 
crazyorange profile image
CrazyOrange

This doesn't work for me. I'll get Error 1053: The service did not respond to the start or control request in a timely fashion.

Collapse
 
dn32 profile image
Marcelo Vieira de Souza

This doesn't work. I'll get Error 1053: The service did not respond to the start or control request in a timely fashion.

Collapse
 
websplee profile image
websplee

Thanks for this straight to the point publication. I have a console app that I struggled to port to a COM application for some PHP client app. So self-hosted windows service to the rescue.

Collapse
 
amitdvs profile image
amitdvs • Edited

It works for me and can host API in window service. I can also run API in localhost.
My question is that now how I can map URL like someting.com in window service.
Please share a hosting doc or article, how I can map localhost to domain in window service.

Collapse
 
bmarcco profile image
bmarcco

Hi, I use .Net Core Web Api with Net Core 5, and i was write Program.cs like code bellow
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
}).ConfigureWebHost(config =>
{
config.UseUrls(“http://*:5050”);

}).UseWindowsService();
}
but, when start my windows service API not working but when run manually helperService.exe it is work perfect. Do you know what the problem may be?

Collapse
 
akashga32571314 profile image
Akash Gaikwad

I have same application which is working fine with HTTP after installing as a windows service.. but now I wanted to enable HTTPS for windows service so how we can do this.. please suggest