How to specify AzureWebJobsStorage in latest azure

2019-07-23 13:31发布

问题:

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();

回答1:

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:



回答2:

I ran into the same problem and here is my solution.

Below are the Azure assemblies that I am referring to

With above setup, there is no need to invoke c.AddJsonFile("appsettings.json", ... ) if name of config file is 'appSettings.json'

By default, framework will look for the file named 'appsettings.json'

However, if name of file is something else the we need to tell HostBuilder what is name of our config file.

HostBuilder builder = new HostBuilder();

//Below piece of code is not required if name of json file is 'appsettings.json'
builder.ConfigureAppConfiguration(c =>
{
   c.AddJsonFile("Myappsettings.json", optional: false, reloadOnChange: true);

});

Simple and important step that cause problem while debugging on my local machine was 'Copy to Output Directory'property of my 'appsettings.json' file. @Ivan Yang has already mentioned this in his answer.

Here is the git hub link of the source code.

Note: I have followed this documentation to implement the code base.