Hi, our logging mechanism uses self-generate message-id to corelate inbound and outbound logs. It works perfectly but
when the backend service is ToFormUrlEncodedContent , we find the variable generated in the inbound policy cannot be relay to outbound policy and raise error.
"Expression evaluation failed. The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)",
"The given key was not present in the dictionary."
The sample code is like this, error occurs in outbound
<policies>
<inbound>
<set-variable name="message-id" value="@(Guid.NewGuid().ToString("N"))" />
<log-to-eventhub logger-id="logger1" partition-id="1">@{
var jsonReq = new JObject(
new JProperty("message-type", "request"),
new JProperty("message-id", context.Variables["message-id"]))
);
return jsonReq.ToString()
}
</log-to-eventhub>
</inbound>
<outbound>
<log-to-eventhub logger-id="logger1" partition-id="2">@{
var jsonRes = new JObject(
new JProperty("message-type", "response"),
new JProperty("message-id", context.Variables["message-id"])) //exception on this line
);
return jsonRes.ToString()
}
</log-to-eventhub>
<set-header name="message-id" exists-action="append">
<value>@(context.Variables["message-id"])</value> //exception on this line
</set-header>
<outbound>
Already read the section "IMessageBody" in the following link , but not sure how to deal with context.variables by preserveContent.
https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions
Need your advise.
Thank you