asp.net-coref#f#-scripting

Is it possible to use ASP.NET Core within an F# fsx script?


Found that the question was pondered a few years ago: https://github.com/dotnet/fsharp/issues/8688

My code looks like

#r "nuget: Microsoft.NETCore.App"
#r "nuget: Microsoft.NETCore.App.Ref"
#r "nuget: Microsoft.AspNetCore"

open Microsoft.AspNetCore.Builder

let builder = WebApplication.CreateBuilder(args)

But that does not work.


Solution

  • This hosts a minimal API in a script:

    #r @"C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\9.0.0\Microsoft.AspNetCore.Http.Abstractions.dll"
    #r @"C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\9.0.0\Microsoft.AspNetCore.dll"
    #r @"C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\9.0.0\Microsoft.AspNetCore.Routing.dll"
    
    open System
    open Microsoft.AspNetCore.Builder
    
    let builder = WebApplication.CreateBuilder()
    let app = builder.Build()
    
    app.MapGet("/time", Func<_>(fun _ -> DateTime.Now)) 
    app.MapGet("/lab", Func<_,_>(fun ([<FromQuery>] n: int) -> {| a=2; n=n |})) 
    
    app.Run()