【问题标题】:Set-Variable policy: Expression return type is not allowedSet-Variable 策略:不允许表达式返回类型
【发布时间】:2019-08-03 03:50:44
【问题描述】:

Azure APIM 策略表达式可以访问具有Variables 属性的context object,该属性应该是IReadOnlyDictionary<string, object> 类型。我应该能够通过set-variable policy 向该字典添加值。我希望能够将任何对象添加到字典中,但是当我尝试添加字符串以外的任何对象时遇到了错误。

例如,当我尝试将其保存到入站策略定义中时:

<set-variable name="regexGroups" value="@(Regex.Match("inputString","regex").Groups)" />

我收到一条错误消息:

X 行 Y 列的元素“set-variable”出错:不允许表达式返回类型“System.Text.RegularExpressions.GroupCollection, System”。

GroupCollection 继承自 object,因此它应该是 Variables 字典中的有效值。为什么这不起作用?

如果我尝试将其显式转换为对象:

<set-variable name="regexGroups" value="@((object)Regex.Match("inputString","regex").Groups)" />

我收到一条错误消息:

X 行 Y 列的元素“set-variable”出错:不允许表达式返回类型“System.Object”

我的语法错了吗?

【问题讨论】:

    标签: azure azure-api-management


    【解决方案1】:

    APIM 表达式中仅允许某些类型作为变量值,object 不是其中之一,以下是目前允许作为变量值的完整列表:

    • System.Boolean
    • System.SByte
    • System.Byte
    • System.UInt16
    • System.UInt32
    • System.UInt64
    • System.Int16
    • System.Int32
    • System.Int64
    • System.Decimal
    • System.Single
    • System.Double
    • System.Guid
    • System.String
    • System.Char
    • System.DateTime
    • System.TimeSpan
    • System.Byte[]
    • 回应
    • Jwt
    • Newtonsoft.Json.Linq.JToken

    【讨论】:

    • face-palm 变量的类型让我很反感,我应该仔细阅读 Set-Variable 的文档。谢谢!
    猜你喜欢
    • 2020-01-26
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2020-10-02
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多