我将Azure Webjobs版本3.x用于EventHub触发器。使用字段“ 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=....."
}
}
答案 0 :(得分:0)
请使用以下代码和设置:
appsettings.json(还记得右键单击appsettings.json文件->单击属性->将“复制到输出目录”设置为“如果更新则复制”):
{
"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");
}
测试结果: