Skip to content

An example of running a Giraffe application as a Windows Service

License

Notifications You must be signed in to change notification settings

BillHally/GiraffeAsAService

Repository files navigation

Giraffe as a service

  1. Install the Giraffe template:
    dotnet new -i "giraffe-template::*"
  2. Create the project from the template:
    mkdir c:\git\GiraffeAsAService
    cd c:\git\GiraffeAsAService
    dotnet new giraffe
  3. Open GiraffeAsAService.fsproj file in an editor, and change:
    1. TargetFramework from netcoreapp2.0 to net461
    2. FrameworkVersion from 2.0.0 to 4.6.1
  4. Add the Nuget package Microsoft.AspNetCore.Hosting.WindowsServices to the project
  5. Edit Program.fs, first to add:
    open Microsoft.AspNetCore.Hosting.WindowsServices
  6. 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
  7. Publish the app:
    dotnet publish -c Release -r win10-x64
  8. 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
  9. 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"
  10. Start the service (also needs to be done as Administrator):
    Start-Service GiraffeAsAService
  11. Test the service:
    start http://localhost:5000/ # This should open in your default browser

References

  1. Dustin Moris Gorski's Giraffe - "A native functional ASP.NET Core web framework for F# developers"
  2. How to run and ASP.Net Core application as a Windows service

About

An example of running a Giraffe application as a Windows Service

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published