22

Could someone please share minimum working ASP.NET Core application project written in F#?

To implement a minimal demo in C#, we have to do the following:

mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new

Then edit project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

Then perform dotnet restore and use the follwoing code:

Startup.cs:

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace aspnetcoreapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.Run(context =>
            {
                return context.Response.WriteAsync("Hello from ASP.NET Core!");
            });
        }
    }
}

Program.cs:

using System;
using Microsoft.AspNetCore.Hosting;

namespace aspnetcoreapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

Then perform dotnet run. Could anybody give me a hint how to do the same thing in F#?

Sergey Homyuk
  • 231
  • 2
  • 5
  • Hi, I thought if you're interested in F# ASP.NET Core web development then you might be interested in this NuGet library: https://github.com/dustinmoris/AspNetCore.Lambda. I also blogged about it here: https://dusted.codes/functional-aspnet-core – dustinmoris Feb 07 '17 at 11:01

2 Answers2

27

I think what you are looking for is --lang switch.

So the only change to get the base F# project will be:

dotnet new --lang fsharp

You can then follow Pavel's answer for the actual ASP.NET logic.

Edit:

By the way, fsharp can be also replaced with f# and fs, as it is mentioned in the PR.

And there is another issue, which proposes changes to dotnet new to allow templates.

Edit 2:

New tooling supports more templates:

Templates                                 Short Name      Language      Tags
--------------------------------------------------------------------------------------
Console Application                       console         [C#], F#      Common/Console
Class library                             classlib        [C#], F#      Common/Library
Unit Test Project                         mstest          [C#], F#      Test/MSTest
xUnit Test Project                        xunit           [C#], F#      Test/xUnit
Empty ASP.NET Core Web Application        web             [C#]          Web/Empty
MVC ASP.NET Core Web Application          mvc             [C#], F#      Web/MVC
Web API ASP.NET Core Web Application      webapi          [C#]          Web/WebAPI
Solution File                             sln                           Solution

Usage example:

X:\>dotnet new mvc -n MyFsharpProject -lang F#
Content generation time: 309.5706 ms
The template "MVC ASP.NET Core Web Application" created successfully.
derwasp
  • 802
  • 8
  • 17
21

I have been exploring new .net core recently and faced the same question. Actually, it's quite easy to do that.

Add F# runtime references into your project.json:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true,
    "compilerName": "fsc",
    "compile": "**/*.fs"
  },
  "dependencies": {
    "Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160509",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
  },
  "tools": {
    "dotnet-compile-fsc": {
      "version": "1.0.0-preview2-*",
      "imports": [
        "dnxcore50",
        "portable-net45+win81",
        "netstandard1.3"
      ]
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "portable-net45+win8",
        "dnxcore50"
      ]
    }
  }
}

Then put code below into your Program.fs:

open System
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http


type Startup() = 
    member this.Configure(app: IApplicationBuilder) =
      app.Run(fun context -> context.Response.WriteAsync("Hello from ASP.NET Core!"))


[<EntryPoint>]
let main argv = 
    let host = WebHostBuilder().UseKestrel().UseStartup<Startup>().Build()
    host.Run()
    printfn "Server finished!"
    0

Just by the way, it's very important to define your Startup class like type Startup() not type Startup. Otherwise Kestrel runtime will crash during startup.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
Pavel S.
  • 1,267
  • 14
  • 19
  • 2
    Please find a demo here: https://github.com/pshirshov/asp-net-core-f-sharp-example – Pavel S. Jul 08 '16 at 21:16
  • 1
    Fantastic! It really works! Can it be deployed to Heroku with any buildpack? – fpawel Aug 04 '16 at 20:03
  • 1
    I have no experience with Heroku, but if Heroku supports .net core apps this definitely should work too. – Pavel S. Aug 05 '16 at 16:25
  • 1
    Hi, I have tried it by the F# compiler does not find the Microsoft.AspNetCore namespace. Any hints ? – Assassin Sep 29 '16 at 12:47
  • 2
    @SzymonSasin The intellisense is not working for me either. Note that if you do a `dotnet run` from the project's folder, then it likely will work. The issue is with Visual Studio. According to [this question and answer](http://stackoverflow.com/questions/37719061/c-sharp-f-interop-support-in-visual-studio-2015-on-net-core) there is still no good support for F# in .net core in VS. =/ – Matt Klein Oct 06 '16 at 04:32