用于将项目添加到服务总线的 Azure API 管理策略 - 应该 return 错误
Azure API Management Policy to add item to service bus - should return error
我有以下 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<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}&sig={1}&se={2}&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>
我 return 向呼叫者发送一个 200,其中包含已发送消息的 ID。
发生错误时我想要发生的是 return 错误而不是 200
我已尝试使用无效 URL 进行测试。这是无效的
/transaction/messages2
但 200 仍然是 returned
当服务总线不可用时,我如何才能return出错?
我们直接检查出站部分的错误:
<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>
我有以下 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<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}&sig={1}&se={2}&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>
我 return 向呼叫者发送一个 200,其中包含已发送消息的 ID。
发生错误时我想要发生的是 return 错误而不是 200
我已尝试使用无效 URL 进行测试。这是无效的
/transaction/messages2
但 200 仍然是 returned
当服务总线不可用时,我如何才能return出错?
我们直接检查出站部分的错误:
<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>