如何从f#

时间:2018-07-07 15:12:42

标签: c# asp.net f#

我正在尝试翻译以下内容:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreDotNetCore2App
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

到f#

我想出了这个

open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting

type Program =     
    static member BuildWebHost =
        fun (args : string []) ->
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build()

    static member Main(args : string []) =    
        BuildWebHost(args).Run() 

但在构建时出现错误:

D:\documents\code\rhea\Program.fs(318,13): error FS0039: The value, namespace, type or module 'WebHost' is not defined.
 Maybe you want one of the following:   IWebHost   WebHostBuilder   WebHostDefaults   WebHostExtensions   Web
D:\documents\code\rhea\Program.fs(323,9): error FS0039: The value or constructor 'BuildWebHost' is not defined. Maybe y
ou want one of the following:   Builder   IWebHost

以下是WebHost的文档: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost?view=aspnetcore-2.1

1 个答案:

答案 0 :(得分:0)

open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting

let createWebHostBuilder args =
   WebHost
       .CreateDefaultBuilder(args)
       .UseStartup<Startup>();

[<EntryPoint>]
let main args =
    CreateWebHostBuilder(args).Build().Run()
    0