適用於:開發人員 |基本 |標準 |保險費
原則會 send-service-bus-message 將訊息傳送至 Azure 服務匯流排佇列或主題。 API 請求可選擇性地轉發給後端服務。
備註
- 此原則目前為預覽版。
- 如需將訊息傳送至 Azure 服務匯流排的背景和必要條件,請參閱 如何從 Azure API 管理 將訊息傳送至 Azure 服務匯流排。
備註
請依照原則陳述式中提供的順序,來設定原則的元素和子元素。 深入了解如何設定或編輯 APIM 原則。
政策聲明
<send-service-bus-message queue-name="service bus queue" topic-name="service bus topic"
namespace="FQDN of service bus namespace" client-id="ID of user-assigned managed identity">
<message-properties>
<message-property name="property-name">property-value</message-property>
<!-- if there are multiple properties, then add additional message-property elements -->
</message-properties>
<payload>"message content"</payload>
</send-service-bus-message>
Attributes
| Attribute | Description | 為必填項目 | 預設 |
|---|---|---|---|
queue-name |
指定要將訊息傳送至的服務匯流排佇列名稱。 允許原則運算式和具名值。
queue-name必須指定 或 topic-name 。 |
否 | N/A |
topic-name |
指定要傳送訊息的服務匯流排主題名稱。 允許原則運算式和具名值。
queue-name必須指定 或 topic-name 。 |
否 | N/A |
namespace |
指定服務匯流排命名空間的完整網域名稱。 允許原則運算式和具名值。 | 否 | N/A |
client-id |
指定使用者指派受控識別的用戶端識別碼,以向服務匯流排進行驗證。 身分識別必須指派 Azure 服務匯流排資料傳送者角色。 允許原則運算式和具名值。 如果沒有指定,則會使用系統指派的身分識別。 | 否 | N/A |
Elements
| 元素 | Description | 為必填項目 |
|---|---|---|
payload |
指定要傳送至服務匯流排的訊息承載。 允許原則運算式和具名值。 | Yes |
message-properties |
子元素的 message-property 集合,可指定要與訊息承載一起傳遞的中繼資料。 每個都 message-property 由名稱值對組成。 允許原則運算式和具名值。 |
否 |
Usage
使用量注意事項
- 您必須預先建立接收訊息的 Azure 服務匯流排佇列或主題。
- 每個原則定義可以使用此原則多次。
範例
將訊息傳送至服務匯流排佇列
在此範例中,包含要求本文的訊息會傳送至服務匯流排佇列。 API 管理 執行個體會使用使用者指派的身分識別進行存取。 然後,請求會轉送至後端服務。
<policies>
<inbound>
<send-service-bus-message queue-name="orders" client-id="00001111-aaaa-2222-bbbb-3333cccc4444" namespace="my-service-bus.servicebus.windows.net">
<payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
</send-service-bus-message>
</inbound>
<backend>
<forward-request timeout="60"/>
</backend>
</policies>
將訊息傳送至服務匯流排主題
在此範例中,包含要求本文的訊息會傳送至服務匯流排主題。 API 管理 實例會使用系統指派的身分識別進行存取。 然後,請求會轉送至後端服務。
<policies>
<inbound>
<send-service-bus-message topic-name="orders" namespace="my-service-bus.servicebus.windows.net">
<payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
</send-service-bus-message>
</inbound>
<backend>
<forward-request timeout="60"/>
</backend>
</policies>
傳送訊息和中繼資料
在此範例中,由要求本文組成的訊息會傳送至服務匯流排主題,並將訊息屬性設定為傳送具有承載的中繼資料。 API 管理 實例會使用系統指派的身分識別進行存取。 然後,請求會轉送至後端服務。
<policies>
<inbound>
<send-service-bus-message topic-name="orders" namespace="my-service-bus.servicebus.windows.net">
<message-properties>
<message-property name="Customer">Contoso</message-property>
</message-properties>
<payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
</send-service-bus-message>
</inbound>
<backend>
<forward-request timeout="60"/>
</backend>
</policies>
發送訊息並立即返回
在此範例中,包含要求本文的訊息會傳送至服務匯流排主題。 API 管理 實例會使用系統指派的身分識別進行存取。
201然後,回應狀態碼會立即傳回給呼叫者。
<policies>
<inbound>
<send-service-bus-message topic-name="orders" namespace="my-service-bus.servicebus.windows.net">
<payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
</send-service-bus-message>
<return-response>
<set-status code="201" reason="Created!" />
</return-response>
</inbound>
</policies>
相關原則
相關內容
如需了解有關政策的更多資訊,請參閱:
- 教學課程:轉換及保護 API
- 政策參考,以獲取政策陳述及其設定的完整清單
- 政策表達
- 設定或編輯原則
- 重複使用原則設定
- 策略程式碼片段儲存庫
- 原則遊樂場存放庫
- Azure API 管理 原則工具組
- 取得 Copilot 協助以建立、說明及疑難排解原則