ServiceStack F# dotnet 核心 3.1 示例

ServiceStack F# dotnet core 3.1 example

有一个 example 用于 .NET 4.5 的简单 ServiceStack F# 应用程序:

open System
open ServiceStack

type Hello = { mutable Name: string; }
type HelloResponse = { mutable Result: string; }
type HelloService() =
    interface IService
    member this.Any (req:Hello) = { Result = "Hello, " + req.Name }

//Define the Web Services AppHost
type AppHost =
    inherit AppSelfHostBase
    new() = { inherit AppSelfHostBase("Hi F#!", typeof<HelloService>.Assembly) }
    override this.Configure container =
        base.Routes
            .Add<Hello>("/hello")
            .Add<Hello>("/hello/{Name}") |> ignore

//Run it!
[<EntryPoint>]
let main args =
    let host = if args.Length = 0 then "http://*:1337/" else args.[0]
    printfn "listening on %s ..." host
    let appHost = new AppHost()
    appHost.Init() |> ignore
    appHost.Start host |> ignore
    Console.ReadLine() |> ignore
    0

我是第一次尝试使用 F#,上面页面上的 F# 示例有点过时了。

我已尝试将 this dotnet core 3.1 C# self-host example 重写为 F#,但我的版本无法正常工作,原因如下:

app.Run(context =>
            {
                context.Response.Redirect("/metadata");
                return Task.FromResult(0);
            });

完整的 dotnet 核心 3.1 F# 自托管示例由于上述 F# 代码的对应错误而无法运行:

namespace MyApp

open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.DependencyInjection
open ServiceStack.Text
open System.Diagnostics
open ServiceStack
open ServiceStack.Common
open System
open System.IO
open System.Reflection
open System.Threading.Tasks
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.DependencyInjection
open Funq
open Microsoft.Extensions.Configuration

type Hello = { mutable Name: string; }
type HelloResponse = { mutable Result: string; }
type HelloService() =
    interface IService
    member this.Any (req:Hello) = { Result = "Hello, " + req.Name }

type AppHost =
    inherit AppHostBase
    new() = { inherit AppHostBase("Hi F#!", typeof<HelloService>.Assembly) }
    override this.Configure (container : Container)  =
        base.Routes
            .Add<Hello>("/hello")
            .Add<Hello>("/hello/{Name}") |> ignore

type Startup() =
    inherit ModularStartup()
        
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        member this.ConfigureServices(services : IServiceCollection ) =
            ()
        
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        member this.Configure(app : IApplicationBuilder (*, env : IWebHostEnvironment *) ) =
            app.UseServiceStack(new AppHost())
            app.Run(
                       fun context ->
                           context.Response.Redirect("/metadata")
                           Task.FromResult(0)
                   )

module Main =
    open System
        [<EntryPoint>]
        let main argv =
            let host = new WebHostBuilder()
            host.UseKestrel()
                .UseUrls("http://localhost:5000/")
                .UseModularStartup<Startup>()
                .Build()
                .Run()
            0

任何人都可以为像上面那样的简单自托管控制台应用程序提供核心 3.1 F# 示例的帮助吗?

我添加了一个空的 .NET Core 3.1 项目模板,您可以使用 x dotnet tool 下载到一个新的空目录中,这样您就可以启动一个新的 F# .NET Core 3.1。 ServiceStack 项目:

$ dotnet tool install --global x 
$ md ProjectName && cd ProjectName
$ x mix init-fsharp
$ dotnet run

这会将 init-fsharp Gist 下载到您的本地目录。