2016-07-08 46 views
21

Qualcuno potrebbe condividere un progetto di applicazione ASP.NET Core minimo scritto in F #?Progetto ASP.NET Core 1.0 F #

Per implementare una demo minimo in C#, dobbiamo fare quanto segue:

mkdir aspnetcoreapp 
cd aspnetcoreapp 
dotnet new 

Quindi modificare 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" 
    } 
    } 
} 

Quindi eseguire dotnet restore e utilizzando questo codice 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(); 
     } 
    } 
} 

Quindi eseguire dotnet run. Qualcuno potrebbe darmi un suggerimento su come fare la stessa cosa in F #?

+0

Ciao, ho pensato che se siete interessati a F # ASP.Sviluppo web di NET Core quindi potresti essere interessato a questa libreria NuGet: https://github.com/dustinmoris/AspNetCore.Lambda. Ho anche scritto un blog qui: https://dusted.codes/functional-aspnet-core – dustinmoris

risposta

21

Ho esplorato recentemente il nuovo core .net e ho affrontato la stessa domanda. In realtà, è abbastanza facile farlo.

Aggiungere F riferimenti # runtime nella vostra 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" 
     ] 
    } 
    } 
} 

Poi mettere codice sottostante nel tuo 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 

Proprio a proposito, è molto importante per definire la classe di avvio come non type Startup()type Startup. Altrimenti il ​​runtime di Kestrel si bloccherà durante l'avvio.

+2

Si prega di trovare una demo qui: https://github.com/pshirshov/asp-net-core-f-sharp-example –

+1

Fantastic ! Funziona veramente! Può essere distribuito a Heroku con qualsiasi buildpack? – fpawel

+1

Non ho esperienza con Heroku, ma se Heroku supporta le app di rete .net questo sicuramente dovrebbe funzionare anche. –

25

Penso che quello che stai cercando è lo switch --lang.

Così l'unico cambiamento per ottenere il # progetto base F sarà:

dotnet new --lang fsharp 

È quindi possibile seguire la risposta di Pavel per la logica di ASP.NET reale.

Edit:

proposito, fsharp può essere anche sostituita con f# e fs, come è menzionato nel PR.

E c'è un altro issue, che propone le modifiche a dotnet new per consentire i modelli.

Edit 2:

nuova Tooling supporta più modelli:

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 

Esempio di utilizzo:

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