gpt4 book ai didi

asp.net-core - 在 .NetCore 3.0 中配置 ApiVersionDescriptions 而不在启动时使用构建服务提供程序

转载 作者:行者123 更新时间:2023-12-03 13:47:40 26 4
gpt4 key购买 nike

我的 Startup.cs 文件中有以下代码来获取和处理我的每个 API 版本描述并将它们添加到我的 Swagger。

var apiVersionDescriptionProvider = services.BuildServiceProvider().GetService<IApiVersionDescriptionProvider>();

// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(setup =>
{
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
setup.SwaggerDoc(
$"MyAPISpecification{description.GroupName}",
new OpenApiInfo()
{
Title = "My API Specification",
Version = description.ApiVersion.ToString(),
});
}

我的理解是,我应该依赖注入(inject) IApiVersionDescriptionProvider 的实现,而不是在我的启动类的 ConfigureServices 方法中使用 BuildServiceProvider,因为这会阻止创建单例的额外副本。

在这个特定示例中我将如何处理这个问题,因为这是配置服务的方法,所以我没有在这一点上构建的实例,该服务可以在不使用构建服务提供程序的情况下使用。

我在 StackOverflow 上的其他地方阅读了有关使用选项的信息,但我看不出该示例在这种情况下如何应用。任何帮助都将不胜感激,因为此 Swagger 配置基于 2019 年底发布的 Pluralsight 视频,我预计该视频是正确的。

提前致谢。

最佳答案

您可以使用实现 IConfigureOptions Interface .有一个示例可以完全按照您在存储库中尝试执行的操作:

namespace Microsoft.Examples
{
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;

/// <summary>
/// Configures the Swagger generation options.
/// </summary>
/// <remarks>This allows API versioning to define a Swagger document per API version after the
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container.</remarks>
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerOptions( IApiVersionDescriptionProvider provider ) => this.provider = provider;

/// <inheritdoc />
public void Configure( SwaggerGenOptions options )
{
// add a swagger document for each discovered API version
// note: you might choose to skip or document deprecated API versions differently
foreach ( var description in provider.ApiVersionDescriptions )
{
options.SwaggerDoc( description.GroupName, CreateInfoForApiVersion( description ) );
}
}

static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription description )
{
var info = new OpenApiInfo()
{
Title = "Sample API",
Version = description.ApiVersion.ToString(),
Description = "A sample application with Swagger, Swashbuckle, and API versioning.",
Contact = new OpenApiContact() { Name = "Bill Mei", Email = "bill.mei@somewhere.com" },
License = new OpenApiLicense() { Name = "MIT", Url = new Uri( "https://opensource.org/licenses/MIT" ) }
};

if ( description.IsDeprecated )
{
info.Description += " This API version has been deprecated.";
}

return info;
}
}
}

它被添加到 Startup.cs作为:

services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();

引用文献: Options pattern in ASP.NET Core/ ASP.NET API Versioning

关于asp.net-core - 在 .NetCore 3.0 中配置 ApiVersionDescriptions 而不在启动时使用构建服务提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756374/

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