2016-07-08 27 views
21

Ai đó có thể chia sẻ dự án ứng dụng ASP.NET Core làm việc tối thiểu được viết bằng F #?Dự án ASP.NET Core 1.0 F #

Để thực hiện một bản demo tối thiểu bằng C#, chúng ta phải làm như sau:

mkdir aspnetcoreapp 
cd aspnetcoreapp 
dotnet new 

Sau đó chỉnh sửa 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" 
    } 
    } 
} 

Sau đó thực hiện dotnet restore và sử dụng mã 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(); 
     } 
    } 
} 

Sau đó thực hiện dotnet run. Ai có thể cho tôi một gợi ý làm thế nào để làm điều tương tự trong F #?

+0

chào, tôi nghĩ nếu bạn quan tâm đến F # ASP.NET Core phát triển web sau đó bạn có thể quan tâm đến thư viện NuGet này: https://github.com/dustinmoris/AspNetCore.Lambda. Tôi cũng viết blog về nó ở đây: https://dusted.codes/functional-aspnet-core – dustinmoris

Trả lời

21

Tôi đã khám phá lõi .net mới gần đây và phải đối mặt với cùng một câu hỏi. Trên thực tế, nó khá dễ dàng để làm điều đó.

Thêm F tài liệu tham khảo # runtime vào bạn 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" 
     ] 
    } 
    } 
} 

Sau đó đặt mã dưới đây vào bạn 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 

Chỉ bằng cách này, nó rất quan trọng để xác định lớp Startup của bạn như type Startup() không type Startup. Nếu không Kestrel thời gian chạy sẽ sụp đổ trong quá trình khởi động.

+2

Vui lòng tìm một bản demo tại đây: https://github.com/pshirshov/asp-net-core-f-sharp-example –

+1

Fantastic ! Nó thật sự có hiệu quả! Nó có thể được triển khai đến Heroku với bất kỳ buildpack nào không? – fpawel

+1

Tôi không có kinh nghiệm với Heroku, nhưng nếu Heroku hỗ trợ các ứng dụng lõi .net này chắc chắn cũng sẽ hoạt động. –

25

Tôi nghĩ rằng những gì bạn đang tìm kiếm là --lang chuyển đổi.

Vì vậy, thay đổi duy nhất để có được những dự án cơ sở # F sẽ là:

dotnet new --lang fsharp 

Sau đó bạn có thể làm theo câu trả lời Pavel cho logic ASP.NET thực tế.

Sửa:

Bằng cách này, fsharp có thể cũng thay thế bằng f#fs, vì nó được đề cập trong PR.

Và có một số khác là issue, đề xuất các thay đổi cho dotnet new để cho phép mẫu.

Chỉnh sửa 2:

dụng cụ mới hỗ trợ nhiều mẫu:

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 

Cách sử dụng Ví dụ:

X:\>dotnet new mvc -n MyFsharpProject -lang F# 
Content generation time: 309.5706 ms 
The template "MVC ASP.NET Core Web Application" created successfully. 
Các vấn đề liên quan