对于我们的一些产品中实现的Azure API Gateway,我遇到了这个问题:
消息表达式计算失败。对象引用未设置为对象的实例。异常类型GatewayError
方法集失败-Variable2
{ "statusCode": 500, "message": "Internal server error", "activityId": "bbcf1e2d-c3bd-4020-8a40-cffd99a7f09e" }这是随机的。根据我的分析,当我从后端API获取ResponseCode为500时或0时,我的策略表达式计算失败,因为我捕获了RequestMethod、ResponseBody、StatusCode Received、RequestBody、RequestURL并将其存储在变量中。
我阅读了一些故障排除文章,并确认在策略下的任何地方都不会缺少基本标记。显然,这可能会导致一些问题。
为了解决这个问题,我现在在设置每个变量时使用了一个条件运算符,但仍然发生了相同的错误。
<choose>
<when condition="@((string)(context.Variables["RequestOperation"]) == "POST")">
<set-variable name="RequestedBody" value="@(context.Request.Body == null ? " Unable to capture":
context.Request.Body.As<string>(preserveContent: true))" />
</when>
<otherwise>
<set-variable name="RequestedQueryParameters" value="@(context.Request.Url.QueryString == null ||
context.Request.Url.QueryString == "" ? "Unable to capture":(string)context.Request.Url.QueryString)" />
</otherwise>
</choose>此代码在入站策略中的基本标签下编写。
策略表达式求值失败,错误消息如下:消息表达式求值失败。对象引用未设置为对象的实例。异常类型GatewayError
方法集失败-Variable2
有人能帮我解决这个问题吗?
发布于 2019-08-06 19:46:44
set-variable2表示您的策略中语句的第二个实例存在问题。我不确定实例数是否也有。您可以通过执行带和不带策略的策略来非常快速地测试,并查看实例计数是否发生了变化。
我通过使用try/catch并设置变量或输出跟踪来调试这些问题,然后使用APIMs Test tool来识别问题。以下是您可以尝试的内容:
`<set-variable name="RequestedQueryParameters" value="@{
try {
return string.IsNullOrEmpty(context.Request.Url.QueryString? "Unable to capture" :(string)context.Request.Url.QueryString);
}
catch (Exception ex)
{
return ex.Message;
}
return string.Empty;
}" />
`HTH。
https://stackoverflow.com/questions/56679239
复制相似问题