Mac本地运行.NET 8隔离模式Azure Timer函数启动失败求助
.NET 8 隔离模式Azure定时触发函数本地调试失败(Mac环境)
同一项目中HTTP触发函数可正常运行,但定时触发函数持续报错failed to acquire host lock lease和The listener for function 'Functions.SendNotificationTimer' was unable to start,日志显示连接127.0.0.1:10000被拒绝。已按Azure本地开发和Azurite官方文档配置,相关信息如下:
错误日志
[2024-03-10T20:42:50.803Z] The listener for function 'Functions.SendNotificationTimer' was unable to start. [2024-03-10T20:42:50.803Z] The listener for function 'Functions.SendNotificationTimer' was unable to start. Azure.Core: Retry failed after 6 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy. (Connection refused (127.0.0.1:10000)) (Connection refused (127.0.0.1:10000)) (Connection refused (127.0.0.1:10000)) (Connection refused (127.0.0.1:10000)) (Connection refused (127.0.0.1:10000)) (Connection refused (127.0.0.1:10000)). Azure.Core: Connection refused (127.0.0.1:10000). System.Net.Http: Connection refused (127.0.0.1:10000). System.Net.Sockets: Connection refused. [2024-03-10T20:43:09.115Z] Host instance '0000000000000000000000008F76DCB3' failed to acquire host lock lease: Azure.Core: Connection refused (127.0.0.1:10000). System.Net.Http: Connection refused (127.0.0.1:10000). System.Net.Sockets: Connection refused. [2024-03-10T20:43:37.473Z] Host instance '0000000000000000000000008F76DCB3' failed to acquire host lock lease: Azure.Core: Connection refused (127.0.0.1:10000). System.Net.Http: Connection refused (127.0.0.1:10000). System.Net.Sockets: Connection refused. [2024-03-10T20:44:03.701Z] Host instance '0000000000000000000000008F76DCB3' failed to acquire host lock lease: Azure.Core: Connection refused (127.0.0.1:10000). System.Net.Http: Connection refused (127.0.0.1:10000). System.Net.Sockets: Connection refused. [2024-03-10T20:44:30.433Z] Host instance '0000000000000000000000008F76DCB3' failed to acquire host lock lease: Azure.Core: Connection refused (127.0.0.1:10000). System.Net.Http: Connection refused (127.0.0.1:10000). System.Net.Sockets: Connection refused. [2024-03-10T20:44:47.967Z] Retrying to start listener for function 'Functions.SendNotificationTimer' (Attempt 8)
项目代码
SendNotificationTimer.cs
using System; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; namespace MyCompany.Notifications { public class SendNotificationTimer { private readonly ILogger _logger; public SendNotificationTimer(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<SendNotificationTimer>(); } [Function("SendNotificationTimer")] public void Run([TimerTrigger("*/30 * * * * *")] 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}"); } } } }
program.cs
using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.DependencyInjection; var host = new HostBuilder() .ConfigureFunctionsWebApplication() .ConfigureServices(services => { services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights(); }) .Build(); host.Run();
notificationFunction.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Exe</OutputType> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <RootNamespace>_78127464</RootNamespace> </PropertyGroup> <ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" /> <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> <ItemGroup> <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> </ItemGroup> </Project>
local.settings.json
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" } }
host.json
{ "version": "2.0", "logging": { "logLevel": { "default": "Debug" }, "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" }, "enableLiveMetricsFilters": true } } }
解决方案
核心原因
定时触发函数需要借助Azure存储的Blob服务存储作业锁和调度状态,127.0.0.1:10000是Azurite Blob服务的默认端口,当前Azurite未正常启动或端口被占用,导致函数无法建立连接。
排查及修复步骤
确认Azurite运行状态
- 终端执行
azurite启动服务,查看是否有报错。若提示端口被占用,指定新端口启动:azurite --blobPort 10001 --queuePort 10002 --tablePort 10003 - 更新
local.settings.json中的存储连接字符串,匹配自定义端口:"AzureWebJobsStorage": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10001/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10002/devstoreaccount1;TableEndpoint=http://127.0.0.1:10003/devstoreaccount1;"
- 终端执行
重装Azurite保证完整性
npm uninstall -g azurite npm install -g azurite优先使用最新稳定版。
临时跳过存储依赖(仅调试用)
若仅需验证定时函数逻辑,可在host.json中添加配置跳过存储(生产环境禁止使用):{ "version": "2.0", "logging": { ... }, "extensions": { "timer": { "scheduleMonitor": { "skip": true } } } }更新Azure Functions Core Tools
确保工具版本与.NET 8兼容,执行更新:brew upgrade azure-functions-core-tools@4
内容的提问来源于stack exchange,提问作者lcj
相关产品推荐
相关产品推荐

