如何解决ServiceStack F#dotnet core 3.1示例
有一个{.3}}用于.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#示例有点过时了。
我试图将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下载到您的本地目录。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。