如何修复 "InvalidOperationException: No event hub receiver named <my eventhub name>"

How to fix "InvalidOperationException: No event hub receiver named <my eventhub name>"

我正在为 EventHub 触发器使用 Azure webjobs 版本 3.x。使用字段 "EventHubConnection" 在 appSettings.json 文件中提供了事件中心连接字符串。但是当我尝试 运行 该函数时,出现以下错误: Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException: '错误索引方法 'Functions.Trigger'' InvalidOperationException:没有名为

的事件中心接收器

Program.cs

var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
    b.AddEventHubs();
});
var host = builder.Build();
using (host)
{
     host.Run();
}

Function.cs:

public static void Trigger([EventHubTrigger("my eventhub name")] EventData message, ILogger logger)
{
    string data = Encoding.UTF8.GetString(message.Body);
    logger.LogDebug(".....");
}

appsettings.json:

{
  "ConnectionStrings": {
    "EventHubConnection": "Endpoint=....."
  }
}

请使用以下代码和设置:

appsettings.json(还记得右击appsettings.json文件->点击属性->将"Copy to output directory"设置为"copy if newer"):

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

Function.cs:

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");
}

测试结果: