-6ren">
gpt4 book ai didi

将项目添加到服务总线的 Azure API 管理策略 - 应返回错误

转载 作者:行者123 更新时间:2023-12-03 01:29:32 25 4
gpt4 key购买 nike

我有以下 APIM 政策:-

<policies>
<inbound>
<base />
<set-variable name="payload" value="@(context.Request.Body?.As<JObject>(true))" />

<!-- Put it onto the service bus -->
<rewrite-uri template="/transaction/messages2" copy-unmatched-params="true" />

<cache-lookup-value key="emlsbsas" variable-name="cachedSasToken" />
<choose>
<when condition="@(context.Variables.GetValueOrDefault&lt;string>("cachedSasToken") == null)">
<cache-store-value key="emlsbsas" value="@{
string resourceUri = "{{Transaction_Uri}}";
string keyName = "{{Transaction_KeyName}}";
string key = "{{Transaction_Key}}";
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + 120);
string stringToSign = System.Uri.EscapeDataString(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format("SharedAccessSignature sr={0}&amp;sig={1}&amp;se={2}&amp;skn={3}",
System.Uri.EscapeDataString(resourceUri),
System.Uri.EscapeDataString(signature), expiry, keyName);
return sasToken;
}" duration="10" />
<cache-lookup-value key="emlsbsas" variable-name="cachedSasToken" />
</when>
</choose>
<set-header name="Authorization" exists-action="override">
<value>@(context.Variables.GetValueOrDefault<string>("cachedSasToken"))</value>
</set-header>
<set-header name="Content-type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<return-response>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var json = new JObject(
new JProperty("id", context.Request.Body?.As<JObject>(true)["id"])
);

return json.ToString();
}</set-body>
</return-response>
</outbound>
<on-error>
<set-header name="ErrorSource" exists-action="override">
<value>@(context.LastError.Source)</value>
</set-header>
<set-header name="ErrorReason" exists-action="override">
<value>@(context.LastError.Reason)</value>
</set-header>
<set-header name="ErrorMessage" exists-action="override">
<value>@(context.LastError.Message)</value>
</set-header>
<set-header name="ErrorScope" exists-action="override">
<value>@(context.LastError.Scope)</value>
</set-header>
<set-header name="ErrorSection" exists-action="override">
<value>@(context.LastError.Section)</value>
</set-header>
<set-header name="ErrorPath" exists-action="override">
<value>@(context.LastError.Path)</value>
</set-header>
<set-header name="ErrorPolicyId" exists-action="override">
<value>@(context.LastError.PolicyId)</value>
</set-header>
<set-header name="ErrorStatusCode" exists-action="override">
<value>@(context.Response.StatusCode.ToString())</value>
</set-header>
<base />
</on-error>
</policies>

我向调用者返回 200,其中包含已发送消息的 ID。

发生错误时我想要发生的是返回错误而不是 200

我尝试使用无效的 URL 进行测试。这是无效的

/交易/消息2

但仍然返回 200

当服务总线不可用时,如何让它返回错误?

最佳答案

我们直接检查出站部分是否有错误:

<outbound>
<base />
<choose>
<when condition="@(context.Response.StatusCode >= 400 && context.Response.StatusCode < 503)">
<set-status code="500" reason="Queue failure" />
</when>
<when condition="@(context.Response.StatusCode == 503)">
<set-status code="503" reason="Service unavailable" />
<set-header name="Retry-After" exists-action="override">
<value>300</value>
</set-header>
</when>
<otherwise />
</choose>
<xml-to-json kind="direct" apply="content-type-xml" consider-accept-header="false" />
</outbound>

关于将项目添加到服务总线的 Azure API 管理策略 - 应返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63609662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com