I am creating an Azure web job in .Net Core 2.1 (via a "console" app in Visual Studio). In this project, I have a static function that reads messages from a queue. Within this function, I need to use connection strings (from my configuration) to write to a database. This is my setup:
Program.cs
class Program
{
static void Main(string[] args)
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
});
builder.ConfigureAppConfiguration((hostContext, config) =>
{
var conf = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build();
config.AddConfiguration(conf);
config.AddEnvironmentVariables();
})
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
});
var host = builder.Build();
using (host)
{
host.Run();
}
}
}
Functions.cs
public class Functions
{
public static void ProcessQueueMessage([QueueTrigger("myqueue")] string message, ILogger logger, IConfiguration configuration)
{
logger.LogInformation(message);
logger.LogInformation(configuration.GetConnectionString("MyDatabase"));
}
}
appsettings.json
{
"ConnectionStrings": {
"MyDatabase": "foo",
"AzureWebJobsDashboard": "foo2",
"AzureWebJobsStorage": "foo3"
}
}
However, when I run this, I get the following error:
Error indexing method 'Functions.ProcessQueueMessage'
Cannot bind parameter 'configuration' to type IConfiguration. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
I am very new to .Net Core, especially the DI pattern. And I believe that is the issue. I also see many examples of how to implement and use the configuration from within the Main
function, but not from within a static helper function like this. How do I properly implement my configuration from within my static function?
ConfigureWebJobs
can you dob.Services.AddConfiguration()
or evenb.Services.AddSingleton<IConfiguration, Configuration>()
? – DavidG