Azure API App Swagger 无法从门户获得

Azure API App Swagger is not available from portal

我创建了一个 Azure API 应用程序并将其部署到 Azure。起初,我从门户网站获取 Swagger 文件没有问题(即通过单击 API 应用程序 blade 上的 "API Definition" 按钮),但在某些时候它停止工作。

我在 App_Start\SwaggerConfig.cs 中启用了 Swagger UI。当我在本地调试时,我可以导航到 http://localhost:12345/Swagger to get to the Swagger UI and download the Swagger file, that all works fine. When I go to the portal, I get a 500 error trying to get to https://microsoft-apiappad6cxxxxxxxxxxxx426c23a66.azurewebsites.net:443/swagger/docs/v1.

我试过删除 API 应用程序并重新部署它...没有成功。我已经尝试开始一个新项目并使用它创建相同的界面并将其部署到 Azure ......每次都有效(当然)。

我将在此处包含 Swagger 文件,以防万一,但我不明白的是:为什么 Swashbuckle 可以在本地工作,但不能在 Azure 中工作?

{
"swagger": "2.0",
"info": {
    "version": "v1",
    "title": "StorefrontApi"
},
"host": "localhost:52912",
"schemes": [
    "http"
],
"paths": {
    "/api/v1/storefront/applications": {
        "get": {
            "tags": [
                "GET"
            ],
            "operationId": "Storefront_Applications",
            "consumes": [],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/aurorastatus": {
        "get": {
            "tags": [
                "GET"
            ],
            "operationId": "Storefront_AuroraStatus",
            "consumes": [],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "auroraSerialNumber",
                    "in": "query",
                    "required": true,
                    "type": "string"
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/order": {
        "post": {
            "tags": [
                "POST"
            ],
            "operationId": "Storefront_Order",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/register": {
        "post": {
            "tags": [
                "POST"
            ],
            "summary": "",
            "operationId": "Storefront_Register",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "description": "",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/removeapplication": {
        "post": {
            "tags": [
                "POST"
            ],
            "summary": "",
            "operationId": "Storefront_RemoveApplication",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "description": "",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    },
    "/api/v1/storefront/updateserialnumber": {
        "post": {
            "tags": [
                "POST"
            ],
            "operationId": "Storefront_UpdateSerialNumber",
            "consumes": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml",
                "application/x-www-form-urlencoded"
            ],
            "produces": [
                "application/json",
                "text/json",
                "application/xml",
                "text/xml"
            ],
            "parameters": [
                {
                    "name": "requestBody",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "string"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/Object"
                    }
                }
            },
            "deprecated": false
        }
    }
},
"definitions": {
    "Object": {
        "type": "object",
        "properties": {}
    }
}

}

这当然会限制在逻辑应用程序中使用 API 应用程序的能力,也会限制 right-click / 将 Azure API 应用程序客户端添加到我的项目中。 (是的,我可以在本地获取Swagger文件,然后在项目中修改使用,但那不是我们要找的流程)

有没有人知道是什么原因导致 API 应用无法生成有效的 Swagger(或无法正确执行 Swashbuckle 代码)?

尝试转到新的 Azure 门户(预览门户),浏览到您的 API 应用并将 应用服务 Plan/Pricing 层 设置为 标准.

当我第一次使用默认的应用服务计划在 Azure 中部署我的 API 应用程序时,我也遇到了问题,似乎没有为 API 应用程序创建免费的实例计划。

我的 API 应用程序在我将计划更改为标准后可以正常工作。 希望这对您有所帮助!

My API App with Swagger in Azure

有用的参考:

Create API App Tutorial

Deploy API App Tutorial

编辑: 在下面的评论中找到了答案。在我更新我的 Swashbuckle NuGet 包后,Swagger 正确显示在 Azure 门户中。

部署版本不行时我用的流程是。

  1. 设置对 public 的访问 - 以消除网关引入的问题。
  2. 检查已知终点 api - https:\|xxxxx\yourapi
  3. 在 web.config
  4. 中打开远程错误
  5. 解决构建中未包含的任何缺失引用(将“复制到输出目录”设置为 true)
  6. 重新部署
  7. 访问 Swagger 端点。
  8. 通过网关重新启用安全性。

这允许您删除多个故障点,并在新的 Api 应用程序中添加额外层之前查看最简单的功能是否有效。

您只需更新 Swashbuckle 版本,一切正常

  • Install-Package Swashbuckle - 版本 5.2.2 不工作
  • Install-Package Swashbuckle - 版本 5.2.1 运行完美

我遇到了同样的问题,通过升级服务计划和更新 nuget 包解决了。

运行 最近发现 Startup.cs 的 Config 方法中的 app.UseSwaggerUI 被包装在 If Debug Compiler 指令中。不确定它是开发人员还是自动化模板的一部分,但认为值得一提。