Azure APIM: check if parameter already exsit in dictionary

39 views Asked by At

In APIM, inside one of the APIs, I have a policy inside 'All Operation' that looks similar to this:

enter image description here

<policies>
    <inbound>
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Variables["xvar"] == null ||  (string)context.Variables["xvar"] == "oneValue")">
                <trace source="info" severity="information">
                    <message>Hi, xvar exist</message>
                </trace>
            </when>
            <otherwise />
        </choose>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

The varibale xvar does exist in few operations in this API, e.g. t1. the policy there will looks similar to this:

<policies>
    <inbound>
        <set-variable name="xvar" value="oneValue" />
        <base />
        <set-backend-service base-url="https://testingtest.com" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

However, This variable doesn't exist in the majority of the operations in this API, e.g. t2. Thus, when I call this operation I get this error:

choose (1.469 ms)
{
    "messages": [
        {
            "message": "Expression evaluation failed.",
            "expression": "context.Variables[\"xvar\"] == null ||  (string)context.Variables[\"xvar\"] == \"oneValue\"",
            "details": "The given key was not present in the dictionary.\r\n   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)"
        },
        "Expression evaluation failed. The given key was not present in the dictionary.\r\n   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)",
        "The given key was not present in the dictionary."
    ]
}

What should I change in the condition in 'all operation' to check if a variable exist in the dictionary or doesn't exist

1

There are 1 answers

1
Markus Meyer On BEST ANSWER

Please check the existence of the variable in All Operations with the following expression:
context.Variables.ContainsKey("xvar")

All operations - Policy:

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Variables.ContainsKey("xvar") && !context.Variables.GetValueOrDefault<string>("xvar").Equals(string.Empty))">
                <trace source="info" severity="information">
                    <message>Hi, xvar exist</message>
                    <metadata name="xvar" value="@(context.Variables.GetValueOrDefault<string>("xvar"))" />
                </trace>
            </when>
            <otherwise />
        </choose>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

t1 - Policy:

<policies>
    <inbound>
        <set-variable name="xvar" value="oneValue" />
        <base />
        <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

t2 - Policy:

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="http://echoapi.cloudapp.net/api" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

This traces the following messages for t1 and t2:

Outbound t1 - choose:

{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.ContainsKey(\"xvar\") && !context.Variables.GetValueOrDefault<string>(\"xvar\").Equals(string.Empty)",
    "value": true
}

Outbound t1 - trace:

{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.GetValueOrDefault<string>(\"xvar\")",
    "value": "oneValue"
}

t1

Outbound t2 - choose:

{
    "message": "Expression was successfully evaluated.",
    "expression": "context.Variables.ContainsKey(\"xvar\") && !context.Variables.GetValueOrDefault<string>(\"xvar\").Equals(string.Empty)",
    "value": false
}

t2