如何从 Azure API 管理调用中调试 500 错误?

How to debug 500 error from Azure API Management call?

我的 API 连接到 Azure 的 API 管理服务。当我尝试调用我的端点之一时,出现以下错误:

{
  "statusCode": 500,
  "message": "Internal server error",
  "activityId": "79c1bef9-a05d-4734-b729-0657c1749e40"
}

我启用了跟踪,这是跟踪 json

{
"traceId": "79c1bef9a05d4734b7290657c1749e40",
"traceEntries": {
    "inbound": [
    {
        "source": "api-inspector",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002259",
        "data": {
        "request": {
            "method": "GET",
            "url": "https://mysite.azure-api.net/partner/api/partner/ClientsActions",
            "headers": [
            {
                "name": "Ocp-Apim-Subscription-Key",
                "value": "..."
            },
            {
                "name": "Connection",
                "value": "Keep-Alive"
            },
            {
                "name": "Host",
                "value": "mysite.azure-api.net"
            }
            ]
        }
        }
    },
    {
        "source": "api-inspector",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002352",
        "data": {
        "configuration": {
            "api": {
            "from": "/partner",
            "to": null,
            "version": null,
            "revision": "1"
            },
            "operation": {
            "method": "GET",
            "uriTemplate": "/api/partner/ClientsActions"
            },
            "user": {
            "id": "1",
            "groups": [
                "Administrators",
                "Developers"
            ]
            },
            "product": {
            "id": "57c59e76ea12f3007f060002"
            }
        }
        }
    },
    {
        "source": "cors",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002544",
        "data": "Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied."
    },
    {
        "source": "choose",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002633",
        "data": {
        "message": "Expression was successfully evaluated.",
        "expression": "context.Request.Url.Query.ContainsKey(\"key\")",
        "value": false
        }
    },
    {
        "source": "set-header",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002744",
        "data": {
        "message": "Expression was successfully evaluated.",
        "expression": "(string)context.User.Id",
        "value": "1"
        }
    },
    {
        "source": "set-header",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002802",
        "data": {
        "message": "Specified value was assigned to the header (see below).",
        "header": {
            "name": "x-client-id",
            "value": "1"
        }
        }
    }
    ],
    "backend": [
    {
        "source": "forward-request",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0002909",
        "data": {
        "message": "Backend service URL is not defined."
        }
    },
    {
        "source": "forward-request",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0004824",
        "data": {
        "messages": [
            null,
            "Backend service URL is not defined."
        ]
        }
    }
    ],
    "outbound": [
    {
        "source": "transfer-response",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0007989",
        "data": {
        "message": "Response headers have been sent to the caller."
        }
    },
    {
        "source": "transfer-response",
        "timestamp": "2017-10-24T21:50:09.6322945Z",
        "elapsed": "00:00:00.0008730",
        "data": {
        "message": "Response body streaming to the caller is complete."
        }
    }
    ]
}
}

"Backend service URL is not defined." 消息在我看来很可疑,但我找不到关于它们可能意味着什么的任何信息。如果非要我猜的话,我会说 API 管理层在与我的真实 API 对话时遇到问题,但我可以直接访问它。

任何人都知道可能会发生什么或我应该看什么?我 运行 直接通过 Azure 提供的开发人员门户进行测试。

贾森

在您的 Swagger 文件中,确保它提到了正确的主机、basePath 和方案条目。 Swashbuckle 生成的 Swagger 文件往往不包含这些文件。

这是一个例子:

{
"swagger": "2.0",
"info": {
    "title": "Your title",
    "version": "1.0",
    "description": "Your description"
},
"host": "server.host.com",
"basePath": "/api",
"schemes": [
    "https"
],
"consumes": [
    "application/json"
],
"produces": [
    "application/json"
],

对 "host"、"basePath" 和 "schemes" 特别感兴趣,并根据您的 API 进行更改。

我遇到了同样的问题。我通过将后端 API URL 放在 API 管理中 API 的 "Web service URL" 上来解决它。因此,在撰写本文时,使用门户的步骤为:

  1. 打开您的 API 管理实例
  2. 打开 APIs blade
  3. Select 你的 API 在名单上
  4. “设置”选项卡 > Web 服务 URL 属性

我在 API 管理中配置端点时遇到了类似的错误。当 API 管理无法验证 SSL 证书根授权链时,就会发生这种情况。如果您使用的是自签名证书,则使用下面的 powershell 跳过端点的证书链验证。

$subscriptionName = "MySubscription"
Get-AzureRmSubscription -SubscriptionName $subscriptionName | Set-AzureRmContext 

$context = New-AzureRmApiManagementContext -resourcegroup 'myResourceGroup' -servicename 'myApiManagementServiceName'
New-AzureRmApiManagementBackend -Context  $context -Url 'https://myService.abc.com/' -Protocol http -SkipCertificateChainValidation $true

希望这能解决您的问题。

如果您需要检查哪些 URL 已设置为跳过证书链验证,请使用以下 powershell 命令 -

Get-AzureRmApiManagementBackend -Context $context

最近遇到了这个问题。正在使用 Swashbuckle.AspNetCore 从 api.The 字段创建 json 文件元数据,例如主机、方案、securityDefinitions 和 apiKeyQuery 由于某种原因而丢失,不知道为什么。但在明确添加这些问题后,问题就解决了。在缺失字段下方

"host": "your api host .com",
"schemes": ["http", "https"],
"securityDefinitions": {
    "apiKeyHeader": {
        "type": "apiKey",
        "name": "Ocp-Apim-Subscription-Key",
        "in": "header"
    },
    "apiKeyQuery": {
        "type": "apiKey",
        "name": "subscription-key",
        "in": "query"
    }
},
"security": [{
    "apiKeyHeader": []
}, {
    "apiKeyQuery": []
}],

我遇到了同样的错误,但意识到我有订阅要求,尽管我没有提供任何安全密钥值。