0
votes

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.

1

1 Answers

0
votes

set-variable[2] indicates there is issue in second instance of statement in your policy. I'm unsure whether instance count also . Something you can test very quickly by executing policy with and without and see if instance count changes.

I debug such issues by using try/catch and setting variable or output trace and then use APIMs Test tool to identify issues. Here is what you can try:

`<set-variable name="RequestedQueryParameters" value="@{
try {
    return string.IsNullOrEmpty(context.Request.Url.QueryString? "Unable to capture" :(string)context.Request.Url.QueryString);
}
catch (Exception ex)
{
    return ex.Message;
}
return string.Empty;
}" />
`

HTH.