ASP.NET Core 1.0 F#项目

前端之家收集整理的这篇文章主要介绍了ASP.NET Core 1.0 F#项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有人可以分享用F#编写的最小工作ASP.NET核心应用程序项目吗?

要在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还原并使用以下代码

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运行。任何人都可以给我一个提示如何在F#中做同样的事情?

解决方法

我最近一直在探索新的.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"
      ]
    }
  },"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类非常重要,比如Startup()类型不要键入Startup。否则,Kestrel运行时将在启动期间崩溃。

原文链接:https://www.f2er.com/netcore/252001.html

猜你在找的.NET Core相关文章