- Visual Studio 2019 project to demo how to host a Web API or WebSockets server locally as a Windows Service.
- Possible uses:
- Replace ActiveX dependencies blocking the migration to modern browsers.
- Use as REST wrapper for legacy 32- or 64-bit DLLs.
- Access local system information such as registry keys or software versions for helpdesks.
- Access specialized hardware such as printers, scanners etc.
- Security:
- There are three obvious attack vectors (the may be more):
- An Internet/Intranet website could try to contact the local server: Cross-domain access would be blocked by the browser by default. CORS should be used to restrict browser access to approved domains (white list), sample included.
- A device on the network could try to access the local server: Use a firewall to protect the port on which the local server is running. Also, a sample MVC filter to allow only requests from localhost is included.
- Virus/trojan on the local machine could try to access the local server to perform actions as the account running the service. This scenario is more difficult to defend, as the attacker already has a process running on the target machine. A pre-shared key or related cryptographic mechanism involving authentication could be used to avoid this attack.
- Create projects
Windows Service
(.NET Framework)ASP.NET Core Web API
(.NET Framework)
- NuGet references
-
Install references from Web API in Windows Service, e.g. for ASP.NET Core 2.0:
Microsoft.AspNetCore
Microsoft.AspNetCore.MVC
-
To run Webhost as Windows Service:
Microsoft.AspNetCore.Hosting.WindowsServices
- Windows Service
-
Rename Service.cs to actual service name, e.g.
FileWebApiSvc
-
Open context menu for FileWebApiSvc.cs, choose View Designer
-
In Properties window, set
ServiceName
toFileWebApiSvc
-
Copy files/folders from Web API to Windows Service and correct namespaces:
- Startup.cs
- Controllers
- appsettings.json
- appsettings.Development.json
- Installer
- Open context menu for FileWebApiSvc.cs, choose View Designer
- Right-click on Designer window, choose
Add Installer
- In the Designer for
ProjectInstaller
, chooseserviceInstaller1
- In Properties window, set
ServiceName
toFileWebApiSvc
- Set Description
- Set DisplayName
- Set StartType
- In the Designer, choose
serviceProcessInstaller1
, setAccount
property toLocalSystem
-
Copy Web API bootstrapping code from
Program.cs
to the same file in the Windows Service, make sure anIWebHost
is returned to be able to call.RunAsService()
. -
Install/uninstall service
-
Open Admin Command Prompt
- cd to folder containing
FileWinSvcWebApi.exe
, e.g. bin<Debug|Release>\
- cd to folder containing
-
Run
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe FileWinSvcWebApi.exe
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe FileWinSvcWebApi.exe /u
- Start/stop service
sc start/stop FileWebApiSvc
- -OR-
net start/stop FileWebApiSvc
- -OR-
- UI:
start services.msc
- Debug start issues
- Event viewer:
eventvwr
- Windows protocols --> Application --> Error with Source
.NET Runtime
- Recompile and restart service, reinstalling not necessary
- Can also debug by starting in VS
- Reference for Win10 (also available for 8.1 in another path under "Windows Kits\8.1"):
- C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd