APIM context.Response.Body 始终为空
APIM context.Response.Body always null
我有一个 APIM 策略来向 Azure 事件中心发送请求和响应:
<outbound>
<log-to-eventhub logger-id="my-logger">@{
var request = context.Request.Body.As<string>(preserveContent: true);
var response = context.Response.Body?.As<string>(preserveContent: true);
var json = new JObject(
new JProperty("DateTimeStamp", DateTime.UtcNow),
new JProperty("IpAddress",context.Request.IpAddress),
new JProperty("RequestBody", request),
new JProperty("ResponseBody", response),
new JProperty("ResponseCode", context.Response.StatusCode)
);
return json.ToString();
}</log-to-eventhub>
<base />
</outbound>
通过 Postman 或任何其他客户端发出的请求显示响应具有正确的正文,但在上面的代码中 context.Response.Body 始终为 null,因此进行了 null 检查。该消息在事件中心中显示正常,请求消息正确但响应为空。
API 上还有其他政策,但这是唯一的出站政策。可能是什么原因造成的?
使用完整的 Trace 消息进行编辑,删除空检查并按照建议使用 JObject。
log-to-eventhub (0.405 ms)
{
"messages": [
{
"message": "Expression evaluation failed.",
"expression": "\n var request = context.Request.Body.As<string>(preserveContent: true);\n var response = context.Response.Body.As<JObject>(preserveContent: true);\n\n var json = new JObject(\n new JProperty(\"DateTimeStamp\", DateTime.UtcNow),\n new JProperty(\"ServiceName\",context.Deployment.ServiceName),\n new JProperty(\"RequestId\",context.RequestId),\n new JProperty(\"IpAddress\",context.Request.IpAddress),\n new JProperty(\"OperationName\",context.Operation.Name),\n new JProperty(\"RequestBody\", request),\n new JProperty(\"ResponseBody\", response.ToString()),\n new JProperty(\"ResponseCode\", context.Response.StatusCode)\n );\n \n return json.ToString();\n ",
"details": "Object reference not set to an instance of an object."
},
"Expression evaluation failed. Object reference not set to an instance of an object.",
"Object reference not set to an instance of an object."
]
}
- 代替
As<string>
尝试使用 As<JObject>
这将 return json JObject.
var response = context.Response.Body.As<JObject>(preserveContent: true);
JObject() Initializes a new instance of the JObject class.
我有一个 APIM 策略来向 Azure 事件中心发送请求和响应:
<outbound>
<log-to-eventhub logger-id="my-logger">@{
var request = context.Request.Body.As<string>(preserveContent: true);
var response = context.Response.Body?.As<string>(preserveContent: true);
var json = new JObject(
new JProperty("DateTimeStamp", DateTime.UtcNow),
new JProperty("IpAddress",context.Request.IpAddress),
new JProperty("RequestBody", request),
new JProperty("ResponseBody", response),
new JProperty("ResponseCode", context.Response.StatusCode)
);
return json.ToString();
}</log-to-eventhub>
<base />
</outbound>
通过 Postman 或任何其他客户端发出的请求显示响应具有正确的正文,但在上面的代码中 context.Response.Body 始终为 null,因此进行了 null 检查。该消息在事件中心中显示正常,请求消息正确但响应为空。
API 上还有其他政策,但这是唯一的出站政策。可能是什么原因造成的?
使用完整的 Trace 消息进行编辑,删除空检查并按照建议使用 JObject。
log-to-eventhub (0.405 ms)
{
"messages": [
{
"message": "Expression evaluation failed.",
"expression": "\n var request = context.Request.Body.As<string>(preserveContent: true);\n var response = context.Response.Body.As<JObject>(preserveContent: true);\n\n var json = new JObject(\n new JProperty(\"DateTimeStamp\", DateTime.UtcNow),\n new JProperty(\"ServiceName\",context.Deployment.ServiceName),\n new JProperty(\"RequestId\",context.RequestId),\n new JProperty(\"IpAddress\",context.Request.IpAddress),\n new JProperty(\"OperationName\",context.Operation.Name),\n new JProperty(\"RequestBody\", request),\n new JProperty(\"ResponseBody\", response.ToString()),\n new JProperty(\"ResponseCode\", context.Response.StatusCode)\n );\n \n return json.ToString();\n ",
"details": "Object reference not set to an instance of an object."
},
"Expression evaluation failed. Object reference not set to an instance of an object.",
"Object reference not set to an instance of an object."
]
}
- 代替
As<string>
尝试使用As<JObject>
这将 return json JObject.
var response = context.Response.Body.As<JObject>(preserveContent: true);
JObject() Initializes a new instance of the JObject class.