Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API as a windows service

So I'm creating a new .Net Framework 4.8 Web API in Visual Studio 2019 and I'm wanting to know how to create the API as a windows service? I can't seem to find any examples or online resources to do so. I can run the API locally in VS and it opens Chrome and shows the responses under the local IIS Server it spins up. How do I take this same project and compile it as a windows service while still using HTTPS?

like image 611
user616 Avatar asked Nov 20 '19 17:11

user616


People also ask

Can we host Web API as Windows Service?

It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework. You need to do following steps in order to self-host a web API. Let's see how to host a simple Web API in console application.

Can a Windows Service call an API?

In this article, we are going to learn how to call a web API from a windows service at a scheduled time. We will create a sample ASP.NET Web API project and publish the project to IIS Manager. Once our API is completed, we will create a windows service that calls our Web API at a scheduled time.

How do I host a web service in Windows Service?

Install and run the serviceOpen Developer Command Prompt for Visual Studio and navigate to the project directory. Type installutil bin\service.exe at the command prompt to install the Windows service. Type services. msc at the command prompt to access the Service Control Manager (SCM).


1 Answers

Web API is fully capable of being self hosted on top of OWIN, and does not require IIS to run.

Web API self hosted is basically just a console app. So the techniques for turning a Web API console app into a Windows Service are the same as for any other .NET console app. You can use a service manager such as NSSM, or create a Windows service project directly (by inheriting from the appropriate classes, pretty messy) or use a library like TopShelf.

Note that it's generally not a good idea to directly expose this self hosted app directly to the public. IIS provides a lot of security benefits out of the box designed to protect against malicious requests. If you're planning to publicly expose it, make sure you stick a proxy in front of it that will fulfill those security needs.

like image 108
mason Avatar answered Oct 19 '22 00:10

mason