- Install the Giraffe template:
dotnet new -i "giraffe-template::*"
- Create the project from the template:
mkdir c:\git\GiraffeAsAService cd c:\git\GiraffeAsAService dotnet new giraffe
- Open
GiraffeAsAService.fsproj
file in an editor, and change:TargetFramework
fromnetcoreapp2.0
tonet461
FrameworkVersion
from2.0.0
to4.6.1
- Add the Nuget package
Microsoft.AspNetCore.Hosting.WindowsServices
to the project - Edit
Program.fs
, first to add:open Microsoft.AspNetCore.Hosting.WindowsServices
- Then change the contents of
main
to:let pathToAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location let contentRoot = Path.GetDirectoryName(pathToAssembly) let webRoot = Path.Combine(contentRoot, "WebRoot") let host = WebHostBuilder() .UseKestrel() .UseContentRoot(contentRoot) .UseIISIntegration() .UseWebRoot(webRoot) .Configure(Action<IApplicationBuilder> configureApp) .ConfigureServices(configureServices) .ConfigureLogging(configureLogging) .Build() if Environment.UserInteractive then host.Run() else host.RunAsService() 0
- Publish the app:
dotnet publish -c Release -r win10-x64
- Copy the publish directory to an appropriate place from which to run the Windows service:
mkdir c:\Services\GiraffeAsAService cp -r bin\Release\net461\win10-x64\publish\* c:\Services\GiraffeAsAService
- Create the Windows service (this needs to be done as Administrator):
New-Service ` -Name GiraffeAsAService ` -BinaryPathName c:\Services\GiraffeAsAService\GiraffeAsAService.exe ` -DisplayName "Giraffe as a Windows Service" ` -Description "Example of running a Giraffe application as a Windows Service"
- Start the service (also needs to be done as Administrator):
Start-Service GiraffeAsAService
- Test the service:
start http://localhost:5000/ # This should open in your default browser
- Dustin Moris Gorski's Giraffe - "A native functional ASP.NET Core web framework for F# developers"
- How to run and ASP.Net Core application as a Windows service