如何修复 "No event hub receiver named <name>"

How to fix "No event hub receiver named <name>"

我正在编写一个 azure webjob,它使用 NET Core 3.1 从 eventhub 读取事件。 我有一个配置文件如下:

{
  "JobHostConfig": {
    "DashboardConnectionString": "",
    "StorageConnectionString": "xx"
  },
  "EventHubConfig": {
    "EventHubConnectionString": "xx",
    "EventHubName": "xx",
    "EventProcessorHostName": "xx",
    "ConsumerGroupName": "xx",
    "StorageConnectionString": "xx",
    "StorageContainerName": "xx"
  }
}

在 Main 方法中,我调用了如下所示的 ConfigureServices 方法:

        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory() + $"\..\..\..\ConfigFiles")
            .AddJsonFile($"applicationConfig.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"applicationConfig.{environment}.json", optional: true, reloadOnChange: true);

        Configuration = builder.AddEnvironmentVariables()
                               .Build();

        services.AddSingleton<IConfigurationRoot>(Configuration);
        services.AddSingleton<IConfiguration>(Configuration);
        services.AddMvcCore();
        services.AddSingleton(GetInstance<EventHubConfig>());
        services.AddSingleton(GetInstance<JobHostConfig>());

我确认在运行时配置在 Configuration 中仅像这样填充:Configuration["EventHubConfig:EventHubName"]。但是我也调试过没有设置环境变量,它的值为null.

所以当我这样做的时候: ProcessEvent([EventHubTrigger("%EventHubName%", ConsumerGroup = "%ConsumerGroupName%", Connection = "%ConnectionString%")] EventData eventData) 我知道 %EventHubName% 没有解决。

此外,当我对这些值进行硬编码时,我得到:没有命名的事件中心接收器。

有人可以告诉我我的注册有什么问题吗?

此外,我在 EventHubTrigger 中用字符串替换了值,并且我得到值不能为空。参数名称:receiverConnectionString

右击 appsettings.json file -> click propertities -> set the "Copy to output directory" 到 "copy if newer"

代码应该是

public static void Trigger([EventHubTrigger("my eventhub name",Connection = "EventHubConnectionString")] EventData message, ILogger logger)
{

    string data = Encoding.UTF8.GetString(message.Body);
    Console.WriteLine(data+";;xxx");
}

让你的appsettings.json像

一样简单
{  
  "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net", 
  "EventHubConnectionString": "Endpoint=sb://xxxx"
}

使用%%格式时,应使用Custom binding expressions.

详情请参考此

如果您还有其他问题,请告诉我。