Azure Function启动失败:未找到dotnet-isolated语言函数
无法启动Azure Function的故障排查思路
错误日志
[2024-04-25T09:22:29.421Z] Found C:\Users\gdifronzo\source_C#\AE.Resp.Device.Sync\ AE.Resp.Device.Sync\AE.Resp.Device.Sync\AE.Resp.Device.Sync.csproj. Using for user secrets file configuration. [2024-04-25T09:22:30.766Z] A host error has occurred during startup operation 'd5c1ddb1-1e25-406a-b64c-ef69bf086ab4'. [2024-04-25T09:22:30.768Z] Microsoft.Azure.WebJobs.Script: Did not find functions with language [dotnet-isolated]. [2024-04-25T09:22:30.786Z] Failed to stop host instance '8db57ba1-9d37-44cd-a3d9-e6e01699e700'. [2024-04-25T09:22:30.788Z] Microsoft.Azure.WebJobs.Host: The host has not yet started. Value cannot be null. (Parameter 'provider')
local.settings.json配置
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", "FhirServerUrl": "https://xxxxxxxxxxxx.azurewebsites.net", "TimeTriggerSchedule": "0 */5 * * *", "MaximumExecutionTime": 21600, "MongoDbConnectionString": "mongodb+srv://xxxxxxxxxxx", "MongoDbThumbprint": "xxxxxxxx" } }
核心代码
Program.cs
IHostBuilder builder = new HostBuilder().ConfigureFunctionsWebApplication(); var configuration = new ConfigurationBuilder() .AddEnvironmentVariables() .AddJsonFile("local.settings.json", true, true) .Build(); X509Certificate2 mongoCertificate; string mongoConnectionString = configuration.GetValue<string>("MongoDbConnectionString") ?? throw new CertificateNotFoundException("Unable to load configuration with key 'MongoDbConnectionString'"); try { mongoCertificate = Utils.LoadCertificate(configuration.GetRequiredSection("MongoDbThumbprint").Value); } catch { throw; } MongoDBService mongoDBService = new(mongoConnectionString, mongoCertificate); builder.ConfigureServices(services => { services.AddApplicationInsightsTelemetryWorkerService(); services.ConfigureFunctionsApplicationInsights(); services.AddSingleton<IDeviceSyncService, DeviceSyncService>(); services.AddSingleton(mongoDBService.CreateRepository<ExecutionParameters>("deviceSync")); }); IHost host = builder.Build(); host.Run();
函数代码
private readonly ILogger _logger = loggerFactory.CreateLogger<DeviceSyncFunction>(); private readonly IDeviceSyncService _deviceSyncService = deviceSyncService; [Function("DeviceSyncFunction")] public void Run([TimerTrigger("%TimerExpression%", RunOnStartup = true)] TimerInfo timerInfo) { try { _logger.LogInformation($"Timer trigger function executed at: {DateTime.UtcNow} UTC"); _deviceSyncService.GenerateDevices(); _logger.LogInformation($"Timer trigger function terminated at: {DateTime.UtcNow} UTC"); } catch (OperationCanceledException opex) { _logger.LogWarning(opex, opex.Message); } catch (FhirOperationException ex) { _logger.LogError(ex, ex.Message, ex.StackTrace); } catch (Exception ex) { _logger.LogCritical(ex, ex.Message); } }
.csproj项目文件
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Exe</OutputType> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <FrameworkReference Include="Microsoft.AspNetCore.App" /> <None Include="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> <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.1"/> <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> <ProjectReference Include="..\AE.Resp.Device.Sync.Model\AE.Resp.Device.Sync.Model.csproj" /> <ProjectReference Include="..\AE.Resp.Device.Sync.Service\AE.Resp.Device.Sync.Service.csproj" /> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> </ItemGroup> <ItemGroup> <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> </ItemGroup> </Project>
故障排查思路
- 检查函数类访问修饰符:
DeviceSyncFunction类必须是public,.NET Isolated模式下宿主只会扫描public类中的函数。 - 修正TimerTrigger配置键:函数中用了
%TimerExpression%,但配置文件里的键是TimeTriggerSchedule,两者不匹配,需统一为同一个键名。 - 调整Program.cs的配置加载方式:
ConfigureFunctionsWebApplication已自动加载local.settings.json,手动创建ConfigurationBuilder可能导致配置冲突,建议通过依赖注入获取配置(比如在ConfigureServices中通过context.Configuration访问)。 - 修复函数构造注入:当前函数中的
_logger和_deviceSyncService是直接赋值的,正确做法是通过构造函数注入实例,而非直接初始化,否则宿主无法正确识别函数。 - 修复.csproj的XML结构:项目文件中存在嵌套的
<ItemGroup>标签(第二个ItemGroup内部又嵌套了一个ItemGroup),这是无效的XML,会导致MSBuild解析错误,需调整为正确的层级结构。 - 清理重建项目:删除bin、obj目录,重新build项目,避免旧编译文件干扰。
- 对比正常项目的依赖与配置:将本地可正常运行的Azure Function项目的.csproj依赖版本、Program.cs结构、函数类实现与当前项目逐一对比,找出差异点。
- 检查环境变量覆盖:确认系统级环境变量中是否存在
FUNCTIONS_WORKER_RUNTIME,避免其覆盖local.settings.json中的配置。
内容的提问来源于stack exchange,提问作者GIovanni
相关产品推荐
相关产品推荐

