C
C#•2mo ago
Tostisto

Azure function connection string from azure app configuration store

Hello, I need some help. I have an Azure Function that is triggered by a Service Bus queue message. One of the parameters is the connection string to the Service Bus. My problem is that I am using Azure App Configuration Store to store shared connection strings between apps. The connection strings are stored in the configuration in-memory like this: configuration = configuration.AddInMemoryCollection(config); However, there is a problem when I try to run the app. The Azure Function is not able to read the connection string from the configuration, and I am getting this error: Service Bus account connection string with name ServiceBus:ConnectionString The function looks like this:
[Function(nameof(test))]
public async Task Run([ServiceBusTrigger("queue", Connection = "ServiceBus:ConnectionString")] ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions)
{
try
{
// Some logic here

await messageActions.CompleteMessageAsync(message);
}
catch (Exception)
{
await messageActions.AbandonMessageAsync(message);
}
}
[Function(nameof(test))]
public async Task Run([ServiceBusTrigger("queue", Connection = "ServiceBus:ConnectionString")] ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions)
{
try
{
// Some logic here

await messageActions.CompleteMessageAsync(message);
}
catch (Exception)
{
await messageActions.AbandonMessageAsync(message);
}
}
Am I able to load the connection string to servicebus from inmemory configuration? Thanks for every response.
33 Replies
Sossenbinder
Sossenbinder•2mo ago
Which version of Azure Functions is that? Isolated?
Tostisto
TostistoOP•2mo ago
Yes, isolated v4
Sossenbinder
Sossenbinder•2mo ago
@TeBeCo We were looking into this stuff a few weeks ago, do you remember the correct setup right away? I think the issue was related to config on the host vs on the worker and where the config binds towards
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Sossenbinder
Sossenbinder•2mo ago
Ah, right, that was the solution we ended up with back then, you added the json explicitly I think
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Sossenbinder
Sossenbinder•2mo ago
@Tostisto Bottom line, if I recall correctly, we were also struggling with getting the actual worker to accept dynamic configuration, it's really hard to tell which process you actually end up configuring. I think the right way to pass configurations to your worker is by using local settings json locally, and env variables on Azure Does it work if you add the connectionstring in your json config instead of in memory?
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
I had no problem when I am using only AddUserSecrets<T>. Also when i am using the memory i can acces the values but I can not use this in azure function connection string trigger. yes it match Also i am a little bit special and the key in the app configuration store are separated by __ some i have for example servicebus_connectionstring and in am parsing it into servicebus:connectionstring I tried to use the AddJsonStream but it does not help
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
this is what i have
var appConfigBuilder = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.Connect(azureAppConfigurationConnectionString)
})
.Build();

var transformedConfig = appConfigBuilder.AsEnumerable()
.GroupBy(k => k.Key.Replace("__", ":"))
.ToDictionary(g => g.Key, g => g.First().Value);

var json = JsonConvert.SerializeObject(transformedConfig, Formatting.Indented);
var jsonFilePath = Path.Combine(Path.GetTempPath(), "test.json");
File.WriteAllText(jsonFilePath, json);
configuration.AddJsonFile(jsonFilePath, optional: true, reloadOnChange: true);
var appConfigBuilder = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.Connect(azureAppConfigurationConnectionString)
})
.Build();

var transformedConfig = appConfigBuilder.AsEnumerable()
.GroupBy(k => k.Key.Replace("__", ":"))
.ToDictionary(g => g.Key, g => g.First().Value);

var json = JsonConvert.SerializeObject(transformedConfig, Formatting.Indented);
var jsonFilePath = Path.Combine(Path.GetTempPath(), "test.json");
File.WriteAllText(jsonFilePath, json);
configuration.AddJsonFile(jsonFilePath, optional: true, reloadOnChange: true);
the json use the :
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
yes
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
yes that is in ideal word. but i have the key in the appconfiguration like this ServiceBuses__ConnectionString
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
so i need to replace __ to : and then the connection in the function will be like ServiceBuses:ConnectionString but this is not working at all
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
I can not change that value in appconfiguration
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
Not at all. The my mess code that i send you replace the __ to : , then create json and load as a json. but then i have still the error Service Bus account connection string with name ServiceBus:ConnectionString 😄
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
I do not mean value, but the key.
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
And it is from historical puposes thats because why i have some__some
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
Ok, Thanks, i will try find better solution for that
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
[2025-03-20T20:16:55.036Z] The 'EventLoggerFunction' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.EventLoggerFunction'. Microsoft.Azure.WebJobs.Host: '%ServiceBuses:ConnectionString%' does not resolve to a value.
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
now local
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Tostisto
TostistoOP•2mo ago
Yes, this works The function work correctly, because we uses the environment variables before. but not we migrate to app configuration store and this happened

Did you find this page helpful?