【问题标题】:Publishing to Azure failed due to not seeing AddSwaggerGen由于没有看到 AddSwaggerGen,发布到 Azure 失败
【发布时间】:2022-02-11 15:41:42
【问题描述】:

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

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

发布遇到错误。确保您的 Startup.cs 应用程序正在从 ConfigureServices 中调用 AddSwaggerGen 为了生成招摇文件。访问 https://go.microsoft.com/fwlink/?LinkId=2131205&CLCID=0x409 了解更多 信息。

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

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();
    }
}

【问题讨论】:

    标签: c# azure asp.net-core asp.net-web-api azure-web-app-service


    【解决方案1】:
    • 解决此问题的一种方法是删除现有 App ,创建新 App 并从旧项目复制代码。检查您的路径是否包含空格

    • 删除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

    【讨论】:

    • 非常感谢!删除路径中的空格(然后清理并重建)并将 Swashbuckle 包升级到最新版本就可以了。到目前为止,它无需添加 global.json 即可工作。再次感谢。
    猜你喜欢
    • 2018-11-11
    • 2023-01-19
    • 2012-11-18
    • 2017-01-30
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多