2016-07-08 114 views
21

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

为了实现在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和使用follwoing代码:

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#中做同样的事情?

+0

您好,我想如果你有兴趣在F#ASP。NET核心Web开发,那么你可能会对这个NuGet库感兴趣:https://github.com/dustinmoris/AspNetCore.Lambda。我也在这里发表了博文:https://dusted.codes/functional-aspnet-core – dustinmoris

回答

21

最近我一直在探索新的.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 

只是顺便说说,它定义像type Startup()无法在启动类是非常重要的type Startup。否则Kestrel运行时将在启动过程中崩溃。

+2

请在这里找到一个演示:https://github.com/pshirshov/asp-net-core-f-sharp-example –

+1

神奇!真的行!它可以用任何buildpack部署到Heroku吗? – fpawel

+1

我对Heroku没有任何经验,但如果Heroku支持.net核心应用程序,这肯定也应该工作。 –

25

我认为你要找的是--lang开关。

所以,唯一的变化来获得原F#项目将是:

dotnet new --lang fsharp 

您可以然后按照帕维尔对实际的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.