我的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."
}
}
]
}
}
未定义"后端服务URL。"消息看起来很可疑,但我找不到任何可能含义的信息。如果我不得不猜测我说API管理问题与我的真实API有关,但我可以直接访问它。
任何人都知道可能会发生什么或者我应该注意什么?我直接通过Azure提供的开发人员门户运行测试。
杰森
答案 0 :(得分:8)
我有同样的问题。我通过在API管理中将后端API URL放在API的“Web服务URL”上来解决它。因此,在撰写本文时,使用门户网站的步骤将是:
答案 1 :(得分:7)
在Swagger文件中,确保它提到了正确的主机,basePath和scheme条目。 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"和"计划"并根据您的API更改这些。
答案 2 :(得分:0)
在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
答案 3 :(得分:0)
最近我遇到了这个问题。正在使用Swashbuckle.AspNetCore从api创建json文件元数据。诸如host,schemes,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": []
}],
答案 4 :(得分:-1)
我有同样的错误,但是意识到我有订阅要求,尽管我没有提供任何安全密钥值。