开发 Azure Function (C#) 时,最好禁用 eventhubs 使用者并仅使用 HTTP POST
http://localhost:$port/admin/functions/$FunctionName
在本地触发事件。这样我们就不会被该主题的“噪音”所困扰,并且可以单独和本地触发我们自己的事件。
是否有一种可配置的方法来仅禁用 EventHub 的事件处理程序/触发器?我不想每次都更改代码来启用/禁用它应该只是本地配置。
您可以通过在
"AzureWebJobs.<Function_Name>.Disabled": "true"
中添加应用程序设置local.settings.json
来禁用特定功能。
我创建了一个带有事件中心触发器和Http触发器函数的函数项目。
Http 触发器:
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult("Welcome to Azure Functions!");
}
事件中心触发器:
[Function(nameof(EventHubFunction))]
public void Run([EventHubTrigger("samples-workitems", Connection = "connectionstring")] EventData[] events)
{
foreach (EventData @event in events)
{
_logger.LogInformation("Event Body: {body}", @event.Body);
_logger.LogInformation("Event Content-Type: {contentType}", @event.ContentType);
}
}
local.settings.json:
通过在 local.settings.json 中添加
AzureWebJobs.Function_Name.Disabled
设置禁用 EventHubTrigger:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureWebJobs.EventHubFunction.Disabled": "true",
"connectionString": "<EventHub_connectionstring>"
}
}
回复:
[2024-11-08T09:42:26.486Z] Azure Functions .NET Worker (PID: 20736) initialized in debug mode. Waiting for debugger to attach...
[2024-11-08T09:42:26.538Z] Worker process started and initialized.
Functions:
Http_Trigger: [GET,POST] http://localhost:7142/api/Http_Trigger
Function EventHubFunction is disabled.
For detailed output, run func with --verbose flag.
[2024-11-08T09:42:31.231Z] Executing 'Functions.Http_Trigger' (Reason='This function was programmatically called via the host APIs.', Id=114e9d63-917d-4fe0-a989-15516c04b847)
[2024-11-08T09:42:31.610Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-11-08T09:42:31.700Z] C# HTTP trigger function processed a request.
[2024-11-08T09:42:31.706Z] Executing OkObjectResult, writing value of type 'System.String'.
[2024-11-08T09:42:31.799Z] Executed 'Functions.Http_Trigger' (Succeeded, Id=114e9d63-917d-4fe0-a989-15516c04b847, Duration=596ms)