使用 F# 的 Swashbuckle:System.InvalidOperationException:'无法找到所需的服务
Swashbuckle with F#: System.InvalidOperationException: 'Unable to find the required services
我在 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
我该如何解决这个问题?
services.AddSwaggerGen(fun c -> c.SwaggerDoc("v1", new Info(Title = "My API", Version = "v1")))
是 return 一个 IServiceCollection
的表达式。包含方法 ConfigureServices
需要 return 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# 中创建了一个新的 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
我该如何解决这个问题?
services.AddSwaggerGen(fun c -> c.SwaggerDoc("v1", new Info(Title = "My API", Version = "v1")))
是 return 一个 IServiceCollection
的表达式。包含方法 ConfigureServices
需要 return 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
缺少前导 /
。