Mac本地运行C#定时触发Azure Function报错:缺少HostEndpoint配置
问题描述
在Mac系统的VSCode中运行基于.NET 8的隔离模式定时触发Azure Function时,终端抛出错误:
Unhandled exception. System.InvalidOperationException: Configuration is missing the 'HostEndpoint' information. Please ensure an entry with the key 'Functions:Worker:HostEndpoint' is present in your configuration.
已安装.NET 8 SDK和Azure Function Core Tools,使用VSCode新建函数模板生成的基础代码,相关文件如下:
Program.cs
using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; var host = new HostBuilder() .ConfigureFunctionsWebApplication() .ConfigureServices(services => { services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights(); }) .Build(); host.Run();
TimeTrigger1.cs
using System; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; namespace Company.Function { public class TimerTrigger1 { private readonly ILogger _logger; public TimerTrigger1(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<TimerTrigger1>(); } [Function("TimerTrigger1")] public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer) { _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); if (myTimer.ScheduleStatus is not null) { _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}"); } } } }
local.settings.json
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" } }
解决方案
该错误源于.NET隔离模式的Azure Function需要明确配置Worker与Host的通信端点,按以下步骤修改即可解决:
- 更新local.settings.json配置
在Values节点中添加两个必要的环境变量:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", "Functions_Worker_HostEndpoint": "http://127.0.0.1:5001", "FUNCTIONS_HOST_PORT": "7071" } }
Functions_Worker_HostEndpoint:指定Worker进程连接Host的固定地址,本地运行时填http://127.0.0.1:5001即可FUNCTIONS_HOST_PORT:指定Function Host的监听端口,默认7071,可按需调整
确认工具版本兼容性
确保Azure Function Core Tools为v4.x版本,.NET 8仅兼容该版本工具链。终端执行func --version可查看版本,若版本过低,重新安装最新版即可。可选:调整Program.cs配置(若问题仍存在)
如果使用ConfigureFunctionsWebApplication仍有异常,可替换为适配非HTTP触发场景的ConfigureFunctionsWorkerDefaults:
var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() .ConfigureServices(services => { services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights(); }) .Build(); host.Run();
验证修改
保存配置后重启本地Function,错误会被消除,定时触发函数将按设定间隔执行并输出日志。
内容的提问来源于stack exchange,提问作者PeteC
相关产品推荐
相关产品推荐

