使用F#的Swashbuckle:System.InvalidOperationException:'无法找到所需的服务

问题描述 投票:2回答:1

我用C#创建了一个新的ASP.NET Core Web Application,然后跟着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

我该如何解决?

c# asp.net-core f# swashbuckle
1个回答
0
投票
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

它缺少一个领先的/

© www.soinside.com 2019 - 2024. All rights reserved.