Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 1.0 F# project

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#?

like image 969
Sergey Homyuk Avatar asked Jul 08 '16 21:07

Sergey Homyuk


2 Answers

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. 
like image 113
derwasp Avatar answered Sep 24 '22 05:09

derwasp


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.

like image 26
Pavel S. Avatar answered Sep 24 '22 05:09

Pavel S.