cache-store-value duration - how to use expression for duration?

Martin Kallukalam 440 Reputation points
2024-09-06T04:11:19.09+00:00

User's image

<cache-store-value key="foo" value="bar" duration="@((string)context.Variables["accesstoken-exp"])" />

When I try to save it, I get an error Cannot implicitly convert type 'string' to 'int'.

How can I specify duration as an expression?
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ben Gimblett 4,560 Reputation points Microsoft Employee
    2024-09-06T14:26:45.2066667+00:00

    Hi - thanks for the question

    The context variables is a generic string,object dictionary

    So long as the value was an integer in the first place (e.g. as a result of an expression which returned an integer) you can read it as such

    context.Variables.GetValueOrDefault<int>("duration")

    Or if the value was added as a string literal

    int.Parse(context.Variables.GetValueOrDefault<string>("duration"))

    Example , note i need the tostring because the value is an int

    <set-variable name="durationseconds" value="@(60)" />
    <set-header name="demo" exists-action="override">
         <value>@(context.Variables.GetValueOrDefault<int>("durationseconds").ToString())</value>
    </set-header>
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. JananiRamesh-MSFT 29,261 Reputation points
    2024-09-09T06:04:05.3433333+00:00

    @Martin Kallukalam Thanks for reaching out. To resolve the error "Cannot implicitly convert type 'string' to 'int'", you need to ensure that the duration parameter is an integer. Here is the corrected policy:

    User's image

    In this example, int.Parse is used to convert the string value of accesstoken back to an integer, which is then used as the duration. This should resolve the type conversion error.

    do let me know incase of further queries, I would be happy to assist you.

    0 comments No comments

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.