Share via

How to resuse variable in set header

NC0202 201 Reputation points
2021-01-28T11:49:09.43+00:00

Hi, we want to use message-id to correlate request/response log and return this message-id to the client for future issue solving.

What bothers us is how we reuse message-id in the set-header

 <policies>  
        <inbound>  
     <set-variable name="message-id" value="@(Guid.NewGuid())" />  
            <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"]))  
        );  
     return jsonRes.ToString()   
     }  
     </log-to-eventhub>  
     <set-header name="message-id" exists-action="append">  
                <value>@(context.Variables["message-id"])</value>  
            </set-header>  
     <outbound>  

Here's what we've tried so far

@(context.Variables["message-id"])
==> can't save . error msg : Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)

@((string)context.Variables["message-id"])
@(context.Variables.GetValueOrDefault<string>("message-id"))
==> can save, but failed when postman call
{ "statusCode": 500, "message": "Internal server error", "activityId": "xxxx" }

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.


Answer accepted by question author

Pramod Valavala 20,661 Reputation points Microsoft Employee Moderator
2021-01-29T10:40:13.497+00:00

Instead of a cast, try using Guid.NewGuid().ToString().

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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