使用.NET core 3.1的Azure webjob

问题描述 投票:0回答:1

我正在使用 .net core 3.1 编写一个 azure webjob,我得到了以下运行时异常:InvalidOperationException: %EventHubName% does not resolve to a value.

我的触发器看起来像。 ProcessEvent([EventHubTrigger("%EventHubName%", ConsumerGroup = "%ConsumerGroupName%")] EventData eventData)

我已经在program.cs中注册了配置文件

我已经添加了appSettings.environment.json文件,其中包含了这样的内容。

"EventHubConfig": { "EventHubConnectionString": "..", "EventHubName": "..", "EventProcessorHostName": "..", "ConsumerGroupName": "..", "StorageConnectionString": "..", "StorageContainerName": ".." },

有人能建议我可能缺少什么吗?

c# azure asp.net-core azure-webjobs azure-eventhub
1个回答
0
投票

更新0515.我的触发器看起来像:......。

解决方法:

1.请同时添加 appsettings.jsonappsettings.dev.json记住右击这些文件-> 选择属性-> 设置 "复制到输出目录 "为 "copy if newer")文件到你的项目中。2个json文件应该有相同的结构(键),但值可以不同。像下面这样。

我的appsettings.json文件

{
  "AzureWebJobsStorage": "xxxxx",
  "EventHubConnectionString": "xxxx",
  "EventHubName": "yyeventhub1",
  "ConsumerGroupName": "cg1"
}

我的appsettings.dev.json文件:

   {
      "AzureWebJobsStorage": "xxxxx",
      "EventHubConnectionString": "xxxx",
      "EventHubName": "yyeventhub2",
      "ConsumerGroupName": "hub2cg"
    }

2.In 自定义名称解析器 类,使用下面的代码。

public class CustomNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile("appsettings.dev.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

        return config[name];
    }
}

2.appsettings.json应该添加在appsettings.dev.json之前,所以appsettings.dev.json中的设置具有较高的优先级,会覆盖appsettings.json中的相同键。

3.然后运行项目,并将事件发送到 yyeventhub2 其中在appsettings.dev.json中定义,那么你可以发现webjob被触发了。


原答案

当使用 %% 在函数中。

根据我的测试,以下是一些注意事项。

1.一些最新的nuget包不能和eventhub触发器一起工作,你应该使用下面版本的nuget包。

<ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
  </ItemGroup>

2.eventhub命名空间的连接字符串不能用这种格式。%EventHubConnectionString% 中的Functions.cs。

3.不要使用appsettings.json中的嵌套设置。

4.不要在Appsettings.json中指定 EventProcessorHostNameStorageContainerName appsettings.json中。webjobs SDK会自动为你设置。

5.当使用%%格式时,应使用 自定义绑定表达式 来解析名称。

以下是我的代码和appsettings.json。

appsettings. json(也请右击此文件->选择属性->将 "复制到输出目录 "设置为 "如果较新则复制")。

{     
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xx;BlobEndpoint=https://xxx.blob.core.windows.net/;TableEndpoint=https://xxx.table.core.windows.net/;QueueEndpoint=https://xxx.queue.core.windows.net/;FileEndpoint=https://xxx.file.core.windows.net/",
    "EventHubConnectionString": "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx",
    "EventHubName": "xxx",
    "ConsumerGroupName": "xxx"  
}

我的NameResolver类(创建一个名为CustomNameResolver.cs的自定义类):

public class CustomNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        //.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

        return config[name];
    }
}

Program.cs.Functions.Cs:程序.cs.Functions.Cs:程序。

class Program
{     

    static void Main(string[] args)
    {
        var builder = new HostBuilder();
        var resolver = new CustomNameResolver();

        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorage();
            b.AddEventHubs();
        });
        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();
        });          

        builder.ConfigureServices(s => s.AddSingleton<INameResolver>(resolver));

        var host = builder.Build();
        using (host)
        {
            host.Run();
        }
    }
}

函数.cs:

public class Functions
{
    public static void ProcessEvent([EventHubTrigger("%EventHubName%", Connection = "EventHubConnectionString",ConsumerGroup = "%ConsumerGroupName%")] EventData eventData, ILogger logger)
    {
        logger.LogInformation("it is triggered!" + DateTime.Now.ToLongTimeString());
    }
}

测试结果:

enter image description here


0
投票

使用 %%XXX%% 是读取设置的正确方式。

当你在本地开发时,你的应用会在 local.settings.json 文件。

例子 :

{
    "EventHubName": "myeventhubname"
}

如果您在 Azure 上部署,您需要将变量添加到 "应用程序设置 "中。阅读更多

© www.soinside.com 2019 - 2024. All rights reserved.