ASP.NET CORE Web API inside a Worker Service

Stéphane Roy 31 Reputation points
2021-12-07T19:33:36.237+00:00

Hi,

I would appreciate if someone can give me some info or references so I can have a ASP.NET Core Web API running as a Worker Service. Basically, a Web Service (without IIS) running as a Worker Service (Windows Service).

I looked a bit on all the usual sites but I can't find a concise example (although fully working).

Thanks in advance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,023 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 52,826 Reputation points
    2021-12-07T19:45:09.873+00:00

    you have two options.

    create a standard asp.net core website and run the command line with srvany.exe

    https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service

    create a asp.net core service that support asp.net core:

    https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio


1 additional answer

Sort by: Most helpful
  1. Stéphane Roy 31 Reputation points
    2021-12-08T13:34:28.55+00:00

    Hi,

    It looks like https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio is the best ref. but I was hoping for a fully working example.

    Anyway, I found a complete example at https://www.growin.com/blog/create-worker-service-api-door-net-core-3-1/. In my case it's targeting .Net 5 and I used the following as a test for a different port (it will be configurable).

                    .ConfigureWebHostDefaults(webBuilder =>  
                    {  
                        webBuilder.UseStartup<Startup>();  
                        webBuilder.UseKestrel(opts =>  
                        {  
                            opts.ListenLocalhost(10005);  
                        });  
                    })  
    

    Also (just in case) I added

    127.0.0.1 localhost

    in C:\Windows\System32\drivers\etc\hosts.

    Thanks again

    0 comments No comments