I am facing this issue for the Azure API Gateway implemented in few of our products :
Message Expression evaluation failed. Object reference not set to an instance of an object.
Exception type GatewayError
Failed method set-variable[2]
{ "statusCode": 500, "message": "Internal server error", "activityId": "bbcf1e2d-c3bd-4020-8a40-cffd99a7f09e" }
This is random. As per my analysis, when I get ResponseCode as 500 or 0 from Back end API, my policy expression evaluation fails where I am capturing RequestMethod, ResponseBody, StatusCode Received, RequestBody, RequestURL and storing the same in a variable.
I read a few troubleshooting articles and have confirmed that the BASE tag is not missing from anywhere under policies. Apparently, this could cause some issues.
Trying to fix the issue, I have now used a conditional operator while setting each variable however the same error still occurs.
<choose>
<when condition="@((string)(context.Variables["RequestOperation"]) == "POST")">
<set-variable name="RequestedBody" value="@(context.Request.Body == null ? " Unable to capture":
context.Request.Body.As<string>(preserveContent: true))" />
</when>
<otherwise>
<set-variable name="RequestedQueryParameters" value="@(context.Request.Url.QueryString == null ||
context.Request.Url.QueryString == "" ? "Unable to capture":(string)context.Request.Url.QueryString)" />
</otherwise>
</choose>
This code is written in the INBOUND policy under the BASE tag.
Policy expression evaluation is failing with the following error message:
Message Expression evaluation failed. Object reference not set to an instance of an object.
Exception type GatewayError
Failed method set-variable[2]
Can someone please help me to solve this problem.