Azure 发布:无法更新 Azure 中的 API

Gor*_*ran 1 c# azure swagger azure-web-app-service

我正在尝试从 Visual Studio 在 Azure 上发布我的 .net3.1 web 应用程序。

Visual Studio 在“开始更新 API”步骤上失败,这是 Visual Studio 的输出:

Build started...
1>------ Build started: Project: WebApplication_XXX, Configuration: Release Any CPU ------
1>WebApplication_XXX -> C:\Users\YYYY\source\repos\WebApplication_XXX\WebApplication_XXX\bin\Release\netcoreapp3.1\WebApplication_XXX.dll
1>Done building project "WebApplication_XXX.csproj".
2>------ Publish started: Project: WebApplication_XXX, Configuration: Release Any CPU ------
WebApplication_XXX -> C:\Users\YYYY\source\repos\WebApplication_XXX\WebApplication_XXX\bin\Release\netcoreapp3.1\WebApplication_XXX.dll
npm install
npm run build -- --prod
> webapplication_xxx@0.0.0 build C:\Users\YYYY\source\repos\WebApplication_XXX\WebApplication_XXX\ClientApp
> ng build "--prod"

Generating ES5 bundles for differential loading...
ES5 bundle generation complete.
....
Publish Succeeded.
Web App was published successfully https://xxxxxxx.azurewebsites.net/
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Publish: 1 succeeded, 0 failed, 0 skipped ==========
Starting to update your API
Generating swagger file to 'C:\Users\YYYY\source\repos\WebApplication_XXX\WebApplication_XXX\bin\Release\netcoreapp3.1\swagger.json'.
Failed to update your API in Azure.
Run Code Online (Sandbox Code Playgroud)

然后我检查 Azure 门户并在“创建 API 或更新 API”json 中发现一些错误

...
"properties": {
    "statusCode": "BadRequest",
    "serviceRequestId": "*****",
    "statusMessage": "{\"error\":{\"code\":\"ValidationError\",\"message\":\"One or more fields contain incorrect values:\",\"details\":[{\"code\":\"ValidationError\",\"target\":\"representation\",\"message\":\"Parsing error(s): JSON is valid against no schemas from 'oneOf'. Path 'securityDefinitions.Bearer', line 2841, position 15.\"},{\"code\":\"ValidationError\",\"target\":\"representation\",\"message\":\"Parsing error(s): The input OpenAPI file is not valid for the OpenAPI specificate https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md (schema https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v2.0/schema.json).\"}]}}",
    "eventCategory": "Administrative",
    "entity": "/subscriptions/*****/resourceGroups/XXX/providers/Microsoft.ApiManagement/service/WebApplicationXXXapi/apis/WebApplicationXXX",
    "message": "Microsoft.ApiManagement/service/apis/write",
    "hierarchy": "*****"
},
...
Run Code Online (Sandbox Code Playgroud)

因此,我在 swagger 编辑器中打开生成的 swagger.json 文件'C:\Users\YYYY\source\repos\WebApplication_XXX\WebApplication_XXX\bin\Release\netcoreapp3.1\swagger.json'并得到相同的错误:

Structural error at securityDefinitions.Bearer
should have required property 'type'
missingProperty: type
Run Code Online (Sandbox Code Playgroud)

因为 json 文件中的 Security Definitions Bearer 为空

securityDefinitions:
  Bearer: {
  }
Run Code Online (Sandbox Code Playgroud)

如果我在招摇编辑器中进行以下更改,它会很高兴:

securityDefinitions:
  Bearer: {
    type: apiKey,
    name: "JWT Authentication",
    in: "header"
  }
Run Code Online (Sandbox Code Playgroud)

在我的应用程序 Startup.cs 中我得到:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "XXX API", Version = "v1" });
    var securityScheme = new OpenApiSecurityScheme
    {
        Name = "JWT Authentication",
        Description = "Enter JWT Bearer token **_only_**",
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.Http,
        Scheme = "bearer", // must be lower case
        BearerFormat = "JWT",
        Reference = new OpenApiReference
        {
            Id = JwtBearerDefaults.AuthenticationScheme,
            Type = ReferenceType.SecurityScheme
        }
    };
    c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);
    c.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {securityScheme, new string[] { }}
    });
});
Run Code Online (Sandbox Code Playgroud)

我缺少什么?Startup.cs中的代码在生成swagger.json文件时是否应该添加securityDefinition?

小智 5

我们有同样的问题。目前,我们使用了一种解决方法,即在发布期间禁用更新 api。(我使用VS2022和.net6.0)
在PublishProfiles/...pubxml中将参数UpdateApiOnPublish更改为false。

<UpdateApiOnPublish>false</UpdateApiOnPublish>
Run Code Online (Sandbox Code Playgroud)