ASP.NET Core 1.0 F#项目

22

请问有人能分享使用F#编写的ASP.NET Core最小工作应用程序项目吗?

为了在C#中实现最小演示,我们需要执行以下操作:

mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new

然后编辑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"
    }
  }
}

然后执行dotnet restore,并使用以下代码:

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();
        }
    }
}

然后执行dotnet run。有人可以给我一个提示如何在F#中做同样的事情吗?


嗨,如果你对F# ASP.NET Core Web开发感兴趣,那么你可能会对这个NuGet库感兴趣:https://github.com/dustinmoris/AspNetCore.Lambda。我也在这里写了一篇博客:https://dusted.codes/functional-aspnet-core。 - dustinmoris
2个回答

27
我认为你需要的是--lang切换。
因此,获取基本的F#项目所需的唯一更改将是:
dotnet new --lang fsharp

你可以按照Pavel的回答来实际编写ASP.NET逻辑。 编辑: 顺便提一下,fsharp也可以替换为f#fs,正如PR中所提到的那样。
还有另一个问题,它提出了对dotnet new进行更改以允许使用模板。 编辑2: 新工具支持更多模板:
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

使用示例:

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

21

我最近一直在探索新的.net core,并遇到了相同的问题。实际上,做到这一点非常容易。

将F#运行时引用添加到你的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"
      ]
    }
  }
}

接着把下面的代码放到你的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

顺便说一下,非常重要的一点是要像这样定义您的 Startup 类:type Startup() 而不是 type Startup。否则,在启动期间 Kestrel 运行时将崩溃。


2
请在此处查看演示:https://github.com/pshirshov/asp-net-core-f-sharp-example - Pavel S.
1
太棒了!它真的有效!它可以使用任何构建包部署到Heroku吗? - fpawel
1
我没有使用过Heroku,但如果Heroku支持.NET Core应用程序,那么这肯定也可以工作。 - Pavel S.
1
你好,我已经尝试过使用 F# 编译器,但它找不到 Microsoft.AspNetCore 命名空间。有什么提示吗? - Assassin
2
@SzymonSasin 我的智能感知也无法工作。请注意,如果您从项目文件夹中运行 dotnet run,那么它可能会正常工作。问题出在 Visual Studio 上。根据这个问题和答案,目前在 VS 中 .net core 对 F# 的支持还不够好。=/ - Matt Klein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接