![](/img/trans.png)
[英]How to access azure service bus message properties in Azure function 2
[英]How can I access the ApplicationProperties of a service bus message in a Azure Function isolated process?
我有一條添加了一些ApplicationProperties
的服務總線消息:
ServiceBusMessage serviceBusMessage
serviceBusMessage.ApplicationProperties.Add("TenantId", tenantId);
serviceBusMessage.ApplicationProperties.Add("Serialization", "JSON");
我需要從我的 Azure function 訪問這些。 在class 庫樣式 function 應用程序中,我可以使用ServiceBusReceivedMessage
但在 out of proc 中似乎沒有等效項?
經過大量挖掘后,我意識到 a) 包含FunctionContext
class的 function 存在過載。
所以我的 function 具有以下簽名:
[Function("ExternalProviderChanged")]
public void ExternalProviderChanged([ServiceBusTrigger("topic",
"subscription",
Connection = "ServiceBus")]
string myQueueItem, FunctionContext context)
b) 在FunctionContext
中,應用程序設置是可用的,盡管非常隱藏。 function 上下文公開了以下context.BindingContext.BindingData
,它是context.BindingContext.BindingData
。 在這個字典里面是一個屬性UserProperties
(是的,舊名稱不是 MS 改成的那個),並且該屬性包含 JSON 格式的ApplicationProperties
。 因此,要獲得財產x
,我必須這樣做:
IReadOnlyDictionary<string, object> bindingData = context.BindingContext.BindingData;
if (bindingData.ContainsKey("UserProperties") == false)
{
throw new Exception("Service bus message is missing UserProperties binding data");
}
string userPropertiesStr = bindingData["UserProperties"].ToString();
if (string.IsNullOrEmpty(userPropertiesStr))
{
throw new Exception("UserProperties is null or empty");
}
JsonDocument json = JsonDocument.Parse(userPropertiesStr);
JsonElement xProp = json.RootElement.GetProperty("x");
string x = serializationProp.GetString();
我可能會在戰斗結束后到達,但有一種簡單的方法可以訪問ApplicationProperties
。 只需用Azure.Messaging.ServiceBus.ServiceBusReceivedMessage
類型替換類型string
(它將只返回消息的內容)。
public async Task OnServiceBusAsync(
[ServiceBusTrigger(topicName: "TOPIC", subscriptionName: "SUBSCRIPTION", Connection = "CONNECTION")]
Azure.Messaging.ServiceBus.ServiceBusReceivedMessage message,
ILogger log)
然后您將能夠訪問特定的自定義屬性
var vcountry = message.ApplicationProperties.GetValueOrDefault("BusinessCountry").ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.