Search code examples
azureazure-webjobsazure-webjobssdk

How to specify AzureWebJobsStorage in latest azure webjob 3.03


I updated my old azure webjob code to package to 3.03 and then it is just not working.

I managed to fix all compile-time errors, but when running locally, it throws this error:

Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException
  HResult=0x80131500
  Message=Error indexing method 'MvQueueProcessorV2.ProcessMVRequest'
  Source=Microsoft.Azure.WebJobs.Host
  StackTrace:
   at Microsoft.Azure.WebJobs.Host.RecoverableException.TryRecover(ILogger logger) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Exceptions\RecoverableException.cs:line 81
   at FE.Toolkit.MvPaaS.WebJob.Program.<Main>(String[] args)

Inner Exception 1:
InvalidOperationException: Storage account 'Storage' is not configured.

For me, this seems to indicate that it can't find setting AzureWebJobsStorage? However, it sleeps nicely in the app.config file. So I assumed that I should put my connection string to appsettings.json, so this is what I did in my appsettings.json:

{
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "xxx",
    "Storage": "yyy"
  }
}

However, it gives me the same error. So how do I set storage for webjob 3.0?

This is my code in program.cs

var builder = new HostBuilder()
              .UseEnvironment("Development")
              .ConfigureWebJobs(b =>
               {
                 b.AddAzureStorageCoreServices()
                   .AddAzureStorage()
                   .AddTimers()
                   .AddFiles()
                   .AddDashboardLogging();
                   })
                  .ConfigureLogging((context, b) =>
                 {
                   b.SetMinimumLevel(LogLevel.Debug);
                   b.AddConsole();
                  })
                   .ConfigureServices(services =>
                  {
                        services.AddSingleton<INameResolver, ConfigNameResolver>();
                  })
               .UseConsoleLifetime();

Solution

  • Please add this line of code in your program.cs:

    .ConfigureAppConfiguration((context, config) => {
                        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    })
    

    I have tested at my side, and works fine.

    code in Program.cs:

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    namespace WebJob1template
    {
        class Program
        {
            static void Main()
            {
    
                var builder = new HostBuilder()
                    .UseEnvironment("Development")
                    .ConfigureAppConfiguration((context, config) => {
                        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    })
                    .ConfigureWebJobs(
                    b =>
                    {
                        b.AddAzureStorageCoreServices()
                        .AddAzureStorage()
                        .AddTimers()
                        .AddFiles();
                        //.AddDashboardLogging();
                    })
                    .ConfigureLogging((context, b) =>
                    {
                        b.SetMinimumLevel(LogLevel.Debug);
                        b.AddConsole();
                    })
                    .UseConsoleLifetime();
    
    
                var host = builder.Build();
    
                using (host)
                {
                    host.Run();
                }
            }
        }
    }
    

    appsettings.json(note that set it's property "Copy to Output Directory" as Copy always):

    {
      "ConnectionStrings": {
        "AzureWebJobsDashboard": "xxxx",
        "AzureWebJobsStorage": "xxxx"
      }
    }
    

    Function.cs:

    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    
    namespace WebJob1template
    {
        public class Functions
        {        
            public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger log)
            {
                //log.WriteLine(message);
                log.LogInformation(message);
            }
        }
    }
    

    The test result:

    enter image description here