In APIM, inside one of the APIs, I have a policy inside 'All Operation' that looks similar to this:
<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

Please check the existence of the variable in All Operations with the following expression:
context.Variables.ContainsKey("xvar")All operations - Policy:
t1 - Policy:
t2 - Policy:
This traces the following messages for t1 and t2:
Outbound t1 - choose:
Outbound t1 - trace:
Outbound t2 - choose: