简体   繁体   English

如何在 Azure Function 隔离进程中访问服务总线消息的 ApplicationProperties?

[英]How can I access the ApplicationProperties of a service bus message in a Azure Function isolated process?

I have a service bus message that has some ApplicationProperties added to it:我有一条添加了一些ApplicationProperties的服务总线消息:

ServiceBusMessage serviceBusMessage
serviceBusMessage.ApplicationProperties.Add("TenantId", tenantId);
serviceBusMessage.ApplicationProperties.Add("Serialization", "JSON");

I need to access these from my Azure function.我需要从我的 Azure function 访问这些。 In a class library style function app I can use ServiceBusReceivedMessage but there doesn't seem to be an equivalent in out of proc ?class 库样式 function 应用程序中,我可以使用ServiceBusReceivedMessage在 out of proc 中似乎没有等效项

After much much digging I realised that the a) there is an overload for the function that includes a FunctionContext class .经过大量挖掘后,我意识到 a) 包含FunctionContext class的 function 存在过载。

so my function has the following signature:所以我的 function 具有以下签名:

[Function("ExternalProviderChanged")]
        public void ExternalProviderChanged([ServiceBusTrigger("topic",
            "subscription",
            Connection = "ServiceBus")]
            string myQueueItem, FunctionContext context)

and b) inside the FunctionContext the application settings are available, though pretty hidden. b) 在FunctionContext中,应用程序设置是可用的,尽管非常隐藏。 The function context exposes the following a context.BindingContext.BindingData which is a context.BindingContext.BindingData . function 上下文公开了以下context.BindingContext.BindingData ,它是context.BindingContext.BindingData Inside this dictionary is a property UserProperties (yes the old name not the one MS changed it to) and that property contains the ApplicationProperties in JSON format.在这个字典里面是一个属性UserProperties (是的,旧名称不是 MS 改成的那个),并且该属性包含 JSON 格式的ApplicationProperties So for me to get property x I had to do:因此,要获得财产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();

I might arrive after the battle but there is an easy way to get access to the ApplicationProperties .我可能会在战斗结束后到达,但有一种简单的方法可以访问ApplicationProperties Just replace the type string (which will return you only the content of the message) with the type Azure.Messaging.ServiceBus.ServiceBusReceivedMessage .只需用Azure.Messaging.ServiceBus.ServiceBusReceivedMessage类型替换类型string (它将只返回消息的内容)。

public async Task OnServiceBusAsync(
    [ServiceBusTrigger(topicName: "TOPIC", subscriptionName: "SUBSCRIPTION", Connection = "CONNECTION")]
    Azure.Messaging.ServiceBus.ServiceBusReceivedMessage message,
    ILogger log)

Then you would be able to get access to a specific custom property然后您将能够访问特定的自定义属性

    var vcountry = message.ApplicationProperties.GetValueOrDefault("BusinessCountry").ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM