Home > Mobile >  How can I access the ApplicationProperties of a service bus message in a Azure Function isolated pro
How can I access the ApplicationProperties of a service bus message in a Azure Function isolated pro

Time:01-18

I have a service bus message that has some ApplicationProperties added to it:

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

I need to access these from my 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?

CodePudding user response:

After much much digging I realised that the a) there is an overload for the function that includes a FunctionContext class.

so my function has the following signature:

[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. The function context exposes the following a context.BindingContext.BindingData which is a 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. So for me to get property x I had to do:

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();
  •  Tags:  
  • Related