'Issue related to accessing dependencies in Azure service bus topic trigger function
I have following service bus trigger azure function.
public class EventTaskTopicTrigger
{
private readonly ConfigSettings _configSettings;
public EventTaskTopicTrigger(ConfigSettings configSettings)
{
_configSettings = configSettings;
}
[FunctionName("EventTaskTriggerTopic")]
public void Run([ServiceBusTrigger(_configSettings.Topic,_configSettings.Subscription,_configSettings.ConnectionString)])
{
}
}
when i try to access the _configSetting object to get topic,subscription and Connectionstring then i am getting following error
object reference is required for the nonstatic field, method, or property 'member
Solution 1:[1]
When running on localhost, configuration is read from local.settings.json file, this allows debugging on localhost. This file is not deployed to Azure when the function is published.
When deployed, configuration is read from environmental variables (from configuration in Azure).
The following code would work both for running from local and also when deployed to Azure. It uses ExecutionContext to get function app directory.
[FunctionName("EventTaskTriggerTopic")]
public static async Task Run(
[ServiceBusTrigger("ServiceBussQueueName", Connection = "ServiceBusConnectionString")]
string myQueueItem,
string messageId,
ILogger log,
ExecutionContext context)
{
BuildConfiguration(context.FunctionAppDirectory);
}
private static void BuildConfiguration(string functionAppDirectory)
{
_configuration = new ConfigurationBuilder()
.SetBasePath(functionAppDirectory)
.AddJsonFile("local.settings.json", optional: true)
.AddEnvironmentVariables()
.Build();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Martin Staufcik |
