Azure / Azure-Functions

1.11k stars 197 forks source link

Azure Function 2.0 DI with Storage Extension and IConfiguration #1368

Open brianmrush opened 4 years ago

brianmrush commented 4 years ago

Trying to run dependency injection for an Azure Function v2 that uses a blob trigger. I am getting the following exceptions after my startup

Microsoft.Azure.WebJobs.Host: Error indexing method 'SomeFunction'. Microsoft.Azure.WebJobs.Extensions.Storage: Method not found: 'System.String Microsoft.Extensions.Configuration.IConfigurationExtensions.GetWebJobsConnectionString(Microsoft.Extensions.Configuration.IConfiguration, System.String)'

If comment out my code in the startup except for the AddDependencyInjection(builder) line, this error goes away. Its related to the IConfiguration

Here is my startup code

public void Configure(IWebJobsBuilder builder) { AddDependencyInjection(builder);

    var executioncontextoptions = builder.Services.BuildServiceProvider()
            .GetService<IOptions<ExecutionContextOptions>>().Value;
    var currentDirectory = executioncontextoptions.AppDirectory;

    var configBuilder = new ConfigurationBuilder()
       .SetBasePath(currentDirectory)
       .AddJsonFile("local.appsettings.json", optional: true, reloadOnChange: true)
       //.AddUserSecrets<Startup>()
       .AddEnvironmentVariables();
    var config = (IConfiguration)configBuilder.Build();
    configBuilder.AddConfiguration(config);

    configBuilder.AddAzureKeyVault(
          config["vaulturl"],
         config["clientid"],
         config["clientsecret"]);
    Configuration = config;
kevink369 commented 4 years ago

I have the same issue when running with a queue trigger.

Create a new azure function with a queue trigger.

Configure the trigger to point at your queue:

[FunctionName("ImportQueueProcessor")]
public void Run([QueueTrigger("importQueue", Connection = "STORAGE_ACCOUNT_CONNECTION_STRING")]string myQueueItem, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}

Add startup.cs as follows:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using MyApp.Project;

[assembly: FunctionsStartup(typeof(Startup))]
namespace MyApp.Project
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = new ConfigurationBuilder()
             .Build();
        }
    }
}

Debug the project.

Microsoft.Azure.WebJobs.Host: Error indexing method 'ImportQueueProcessor'. Microsoft.Azure.WebJobs.Extensions.Storage: Method not found: 'System.String Microsoft.Extensions.Configuration.IConfigurationExtensions.GetWebJobsConnectionString(Microsoft.Extensions.Configuration.IConfiguration, System.String)'.

brianmrush commented 4 years ago

Here is my workaround.

https://bmrtechnology.com/2019/10/24/azure-functions-2-0-dependency-injection-azure-key-vault-configuration/

From: kevink369 notifications@github.com Sent: Tuesday, October 29, 2019 12:50 PM To: Azure/Azure-Functions Azure-Functions@noreply.github.com Cc: Brian Rush brianmrush@bmrtechnology.com; Author author@noreply.github.com Subject: Re: [Azure/Azure-Functions] Azure Function 2.0 DI with Storage Extension and IConfiguration (#1368)

I have the same issue when running with a queue trigger.

Create a new azure function with a queue trigger.

Configure the trigger to point at your queue:

[FunctionName("ImportQueueProcessor")] public void Run([QueueTrigger("importQueue", Connection = "STORAGE_ACCOUNT_CONNECTION_STRING")]string myQueueItem, ILogger log) { log.LogInformation($"C# Queue trigger function processed: {myQueueItem}"); }

Add startup.cs as follows:

using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; using MyApp.Project;

[assembly: FunctionsStartup(typeof(Startup))] namespace MyApp.Project { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { var config = new ConfigurationBuilder() .Build(); } } }

Debug the project.

Microsoft.Azure.WebJobs.Host: Error indexing method 'ImportQueueProcessor'. Microsoft.Azure.WebJobs.Extensions.Storage: Method not found: 'System.String Microsoft.Extensions.Configuration.IConfigurationExtensions.GetWebJobsConnectionString(Microsoft.Extensions.Configuration.IConfiguration, System.String)'.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/Azure-Functions/issues/1368?email_source=notifications&email_token=AABPIFUJEXF5LJLUR4FGVU3QRBSSNA5CNFSM4JB2ES62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECRIEWY#issuecomment-547521115 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AABPIFUKJHQEUYX7S5ZCK43QRBSSNANCNFSM4JB2ES6Q . https://github.com/notifications/beacon/AABPIFXKI2E4UV5FPFMVALLQRBSSNA5CNFSM4JB2ES62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECRIEWY.gif

vichakorn commented 4 years ago

I am getting the same exceptions

rustamm commented 4 years ago

I had the same problem. In my case it happened because when I start my Azure Functions V2 project locally it uses DLLs such as Microsoft.Azure.WebJobs.Host.dll from Azure Functions CLI Tools installation folder, e. g. C:\Users\<username>\AppData\Local\AzureFunctionsTools\Releases\2.45.0\cli_x64. These DLLs reference many other DLLs, including Microsoft.Extensions.Configuration.dll. In my case this DLL had version 2.2.0.

If your project directly or indirectly references Microsoft.Extensions.Configuration, make sure that it references version 2., not 3., otherwise you might get this Method not found error because IConfiguration type would come from two different assemblies with different versions while GetWebJobsConnectionString accepts only one of them.

MarleneHE commented 4 years ago

I concur with @rustamm. In my case, the problem came from a NuGet package that was forcing Microsoft.Extensions.Configuration to update to version 3.x.x, which is not comptable with Azure Functions V2. So whenever you install a package, make sure that the version that you select is not going to update this DLL.