由于未看到 AddSwaggerGen,发布到 Azure 失败

Publishing to Azure failed due to not seeing AddSwaggerGen

我有一个简单的 ASP.NET Core 3.1 应用程序,我正在尝试将其发布到 Azure。

但是发布失败并出现以下错误:

Publish has encountered an error. Be sure that the Startup.cs for your application is calling AddSwaggerGen from within ConfigureServices in order to generate swagger file. Visit https://go.microsoft.com/fwlink/?LinkId=2131205&CLCID=0x409 for more information.

但它配置正确并且在本地启动时运行良好。因此我不确定哪里出了问题或如何传递此错误。

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddDbContext<TestProductContext>(options =>
        {
            options.UseSqlServer(Configuration["ConnectionStrings:Database"]);
        });
        services.AddControllers().AddOData(opt =>
            opt.Filter().Expand().Select().OrderBy().Count().SetMaxTop(100)
                .AddRouteComponents("odata", GetEdmModel()));

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "OdataTestProject", Version = "v1" });
            c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
        });

        //// https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#filter-scopes-and-order-of-execution
        services.AddMvc(opts => { opts.Filters.Add(new AutoLogAttribute()); });
        services.AddApplicationInsightsTelemetry();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "/swagger/{documentName}/swagger.json";
            });
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "OdataTestProject v1"));
        }
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }


    private static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        var entitySet = builder.EntitySet<TestProduct>("TestProducts");
        entitySet.EntityType.HasKey(entity => entity.Id);
        return builder.GetEdmModel();
    }
}
  • 解决此问题的一种方法是删除现有的应用程序,创建新的应用程序并从旧的 project.Check 复制代码,如果您的路径包含任何空格

  • 删除 binobj 文件夹。重新打开项目并让 NuGet 在后台加载(它已删除所有 NuGet 引用)。进行清理和重建
  • 尝试使用最新版本Swashbuckle.AspNetCore.
  • 将 global.json 文件添加到您的 Web API 项目
    把这个放进去:
 { 
  "sdk":
   { 
    "version": "3.1.406", 
    "rollForward": "latestPatch" 
   } 
 }
  • 而不是 Swashbuckle.AspNetCore.Swagger nuget 包安装 **Swashbuckle.AspNetCore **.

  • Swashbuckle.AspNetCore. 包包含以下内置的 nuget 包

更多信息请参考git hub