【问题标题】:ASP.NET Core 1.0 F# projectASP.NET Core 1.0 F# 项目
【发布时间】:2016-07-08 21:07:41
【问题描述】:

有人可以分享用 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# 中做同样的事情?

【问题讨论】:

标签: .net f# asp.net-core


【解决方案1】:

我想你要找的是--lang switch。

因此,获得基础 F# 项目的唯一更改是:

dotnet new --lang fsharp

然后,您可以按照 Pavel 对实际 ASP.NET 逻辑的回答。

编辑:

顺便说一句,fsharp也可以替换成f#和fs,正如PR中提到的那样。

还有另一个issue,它建议对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.

【讨论】:

    【解决方案2】:

    我最近一直在探索新的 .net 核心并面临同样的问题。实际上,这很容易做到。

    将 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 运行时会在启动时崩溃。

    【讨论】:

    • 太棒了!真的行!可以使用任何 buildpack 将其部署到 Heroku 吗?
    • 我没有使用 Heroku 的经验,但如果 Heroku 支持 .net 核心应用程序,这肯定也可以。
    • 你好,我试过F#编译器没有找到Microsoft.AspNetCore命名空间。有什么提示吗?
    • @SzymonSasin 智能感知也不适合我。请注意,如果您从项目文件夹中执行dotnet run,那么它可能会起作用。问题在于 Visual Studio。根据this question and answer 的说法,VS 中的 .net core 中仍然没有对 F# 的良好支持。 =/
    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 2016-08-04
    • 2016-11-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    相关资源
    最近更新 更多