ASP.NET Core with F#

While I’m making my way through the tome that is Expert F# 4.0 I’m itching to create some actual code so I thought I’d try making a basic web API using ASP.NET Core.

.NET Core seems to be evolving quite rapidly so it’s difficult to know what is currently considered the ‘right’ way of doing this. I’ve used the command line tools from powershell to create a new project in an empty folder:

dotnet new -l F#

I then created a Core Web Application project in C#, choosing the Web Api template, to use as the basis for porting to F#. After porting the necessary sections across to F# I ended up with a project as follows:

aspnetcore3project

project.json

{
    "version": "1.0.0-*",
    "buildOptions": {
        "debugType": "portable",
        "emitEntryPoint": true,
        "compilerName": "fsc",
        "compile": {
            "includeFiles": [
                "Controllers/FirstController.fs",
                "Program.fs"
            ]
        },
        "define": []
    },
    "tools": {
        "dotnet-compile-fsc": "1.0.0-preview2-*"
    },
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.0.1"
                },
                "Microsoft.FSharp.Core.netcore": "1.0.0-alpha-161102",
                "Microsoft.AspNetCore.Mvc": "1.0.1",
                "Microsoft.AspNetCore.Routing": "1.0.1",
                "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
                "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
            }
        }
    }
}

Program.fs

open System
open System.IO
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.DependencyInjection

type Startup(env: IHostingEnvironment) =

    member this.ConfigureServices(services: IServiceCollection) =
        services.AddMvc() |> ignore

    member this.Configure (app: IApplicationBuilder) =
        app.UseMvc() |> ignore

[<EntryPoint>]
let main argv = 
    printfn "Starting"
    let host = WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseStartup<Startup>().Build()
    host.Run()
    0 //exit code

FirstController.fs

namespace aspnetcore3.Controllers

open System
open System.Collections.Generic
open Microsoft.AspNetCore.Mvc

[<Route("api/first")>]
type FirstController() =
    inherit Controller()

    [<HttpGet>]
    member this.Get() =
        ["x"; "z"]

And with that, browsing to http://localhost:5000/api/first gave me the intended result of returning [“x”,”z”].

I had some problems with F# in Visual Studio 2015 Update 3, and it’s not clear to me whether these are general problems with F# and .NET Core or something I haven’t set up right. The issues I had were:

  • Intellisense doesn’t work with the AspNetCore libraries, so I forever have squiggly red error lines under my open Microsoft.AspNetCore.Mvc statements
  • The compiler doesn’t seem to know when a file has changed, so I was always forcing the build using rebuild or by directly using the command line (\projectdir> dotnet build)
  • I couldn’t get the debugger to work. I was always left with “No symbols have been loaded for this document” and there is a .pdb file in the bin folder. [Edit: F# for CoreCLR status currently indicates Enable CoreCLR debugging of F# is not started, so that may be the problem.

One thought on “ASP.NET Core with F#

  1. Pingback: Tidier Controllers – winter limelight

Comments are closed.