gpt4 book ai didi

c# - 带有 F# : System. InvalidOperationException 的 Swashbuckle:'无法找到所需的服务

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:32 29 4
gpt4 key购买 nike

我在 C# 中创建了一个新的 ASP.NET Core Web 应用程序 并遵循 Get started with Swashbuckle and ASP.NET Core .效果很好。

我在 F# 中做了同样的事情并遇到了这个:

System.InvalidOperationException: 'Unable to 
find the required services. Please add all
the required services by calling
'IServiceCollection.AddMvc' inside the call
to 'ConfigureServices(...)' in the
application startup code.'

这是Startup.fs

namespace ASPNETSwagger

open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Swashbuckle.AspNetCore.Swagger

type Startup () =
new (configuration: IConfiguration) as this =
Startup() then
this.Configuration <- configuration

member _this.ConfigureServices(services: IServiceCollection) =
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2) |> ignore
services.AddSwaggerGen(fun c -> c.SwaggerDoc("v1", new Info(Title = "My API", Version = "v1")))

member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment) =
app.UseSwagger() |> ignore
app.UseSwaggerUI(fun c -> c.SwaggerEndpoint("swagger/v1/swagger.json", "My API V1")) |> ignore

if (env.IsDevelopment()) then
app.UseDeveloperExceptionPage() |> ignore
else
app.UseHsts() |> ignore

app.UseHttpsRedirection() |> ignore
app.UseMvc() |> ignore

member val Configuration : IConfiguration = null with get, set

我该如何解决这个问题?

最佳答案

services.AddSwaggerGen(fun c -> c.SwaggerDoc("v1", new Info(Title = "My API", Version = "v1")))

是一个返回 IServiceCollection 的表达式。包含方法 ConfigureServices 需要返回 unit。因此,将 |> ignore 添加到该行的末尾或将 () 作为最后一行来修复签名。

此外,这一行

app.UseSwaggerUI(fun c -> c.SwaggerEndpoint("swagger/v1/swagger.json", "My API V1")) |> ignore

需要

app.UseSwaggerUI(fun c -> c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1")) |> ignore

缺少前导 /

关于c# - 带有 F# : System. InvalidOperationException 的 Swashbuckle:'无法找到所需的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140398/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com