如何在最新的azure webjob 3.03中指定AzureWebJobsStorage

2024-04-24

我将旧的 azure webjob 代码更新为打包到 3.03,然后它就不起作用了。

我设法修复了所有编译时错误,但在本地运行时,它会抛出以下错误:

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.

对我来说,这似乎表明它找不到设置AzureWeb作业存储?然而,它在 app.config 文件中睡得很好。所以我认为我应该将连接字符串放入 appsettings.json,所以这就是我在 appsettings.json 中所做的:

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

但是,它给了我同样的错误。那么如何设置webjob 3.0的存储呢?

这是我在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();

请在您的program.cs中添加这行代码:

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

我在我身边测试过,效果很好。

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(请注意,将其属性“复制到输出目录”设置为“始终复制”):

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

函数.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);
        }
    }
}

测试结果:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在最新的azure webjob 3.03中指定AzureWebJobsStorage 的相关文章

随机推荐