简体   繁体   中英

APIM context.Response.Body always null

I have an APIM policy to send a request and response to an Azure Event Hub:

    <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>

A request made via Postman or any other client shows the response has the correct body but in the code above context.Response.Body is always null, hence the null check. The message shows up in the event hub fine with a correct request message but null response.

There are other policies on the API but this is the only outbound policy. What could be causing this?

Edit with full Trace message taking out the null check and using JObject as suggested.

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."
    ]
}
  • In place of As<string> try using As<JObject> which would return json JObject.
var response = context.Response.Body.As<JObject>(preserveContent: true);

JObject() Initializes a new instance of the JObject class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM