How to preserve context.Variables when request body is ToFormUrlEncodedContent

NC0202 201 Reputation points
2023-03-09T08:56:35.2733333+00:00

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

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sonny Gillissen 3,751 Reputation points Volunteer Moderator
    2023-03-10T21:24:02.97+00:00

    Hi NC0202

    Thanks for reaching out on Microsoft Q&A!

    When looking at the documentation, the variables live in the context of the operation thus is available in both inbound and backend:

    https://learn.microsoft.com/en-us/azure/api-management/set-variable-policy

    Due to your specific ask please share the code (be it anonymised) for deeper troubleshooting. I can add it in my testlab to perform additional analysis.

    Please click “accept answer” afterwards if you think my response was helpful. For now drop additional queries and information in the comments please.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.