代码之家  ›  专栏  ›  技术社区  ›  GrahamB

Azure web作业.net核心禁用属性应用程序设置不起作用

  •  0
  • GrahamB  · 技术社区  · 5 年前

    .net核心web作业的文档表明:

    [禁用(”消息。已禁用")] 公共异步任务执行同步(。。

    应查找“消息。已禁用如果设置为1或True(不区分大小写),则它将不运行webjob。情况似乎并非如此。

    {
        "AppSettings": {
            "messages.disabled": "true"
        {
    }
    

    但这似乎被忽略了,我的webjob却毫无顾忌地运行。我尝试过“true”、“true”、“1”,并将设置放在JSON应用程序设置文件的根目录中。

    代码编译了文件,但似乎没有按预期工作。我使用的是.net核心2.1,Microsoft.Azure.WebJobs3.0.14款

    我做错什么了?

    0 回复  |  直到 5 年前
        1
  •  1
  •   George Chen    5 年前

    首先是关于 禁用 appsettings.json 文件,格式如下。记住设置 CopyToOutputDirectory 属性到 Copy always Copy if newer .

    enter image description here

    应用设置.json:

    {
      "AzureWebJobsStorage": "storage account connection string",
      "Disable": true
    
    }
    

    enter image description here

    "AzureWebJobs.YourFunctionName.Disabled": "true"

    enter image description here

        2
  •  1
  •   GrahamB    5 年前

    根据@GeorgeChen的一些建议,我认为azureweb作业的禁用处理有一个bug。

    如果将disable属性添加到应用设置.json然后就被忽略了。这是不切实际的,因为在大多数情况下,您不希望将这些设置发布到Azure,而Azure总是这样应用设置.json是-它们在本地运行/调试时更有用,因此可以为那些您不感兴趣的订阅关闭消息处理程序。

    在你的webjob中有一个有效的解决方法program.cs/启动例程,像往常一样构建IConfigurationRoot。通常,此构建将从应用程序设置.config, appSettings.Development.config应用程序设置.Development.config以及appSettings.local.config合并结果。

    接下来,设置一个环境变量。然后,它似乎被disable属性拾取。

    var environment = GetEnvironment();
    
    var cfg = new ConfigurationBuilder()
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appsettings.json", false, true)
                    .AddJsonFile($"appsettings.{environment}.json", true)
                    .AddJsonFile("appsettings.local.json", true)
                    .AddEnvironmentVariables()
                    .Build();
    
    Environment.SetEnvironmentVariable("messages.examplex.disabled", cfg.GetValue<string>("messages.examplex.disabled"));
    

    Web作业方法签名:

    [Disable("messages.examplex.disabled")]
    public async Task ExecuteAsync([ServiceBusTrigger("examplex", "subscribertest")]Message msg)