我无法成功启动我的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"
}
}
Azurite 本地模拟器已打开。我的 Azure Function 项目中的 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);
}
}
项目文件.cproj是:
<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>
我什至不知道发生了什么。正如我所说,蓝铜本地模拟器已打开,我对此没有任何问题。 ChatGPT 表示配置正确。我正在尝试排除故障,但找不到任何明确的信息。当我尝试使用相同的设置和依赖项启动另一个 Azure Functions 时(我已复制并过去了项目文件的依赖项,并且 Program.cs 和 local.settings 也类似),我成功了。所以这个问题似乎不是我本地计算机上隐藏的错误设置。有什么想法吗?
bin
和 obj
文件夹并重建您的函数项目。我已经在我的环境中使用相同的设置测试了您的代码,并且它按预期工作。
将
local.settings.json
代码片段移至 host.json
中 .csproj
的 ItemGroup:
<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" />
<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.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>Always</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>
控制台响应:
[2024-04-25T11:26:48.597Z] Azure Functions .NET Worker (PID: 30604) initialized in debug mode. Waiting for debugger to attach...
[2024-04-25T11:26:48.721Z] Worker process started and initialized.
Functions:
Function1: timerTrigger
For detailed output, run func with --verbose flag.
[2024-04-25T11:26:49.467Z] Executing 'Functions.Function1' (Reason='Timer fired at 2024-04-25T16:56:49.4150207+05:30', Id=957b8027-bef6-4bc4-af26-438c96c1bef8)
[2024-04-25T11:26:49.471Z] Trigger Details: UnscheduledInvocationReason: RunOnStartup
[2024-04-25T11:26:49.748Z] Timer trigger function terminated at: 4/25/2024 11:26:49 AMUTC
[2024-04-25T11:26:49.748Z] Timer trigger function executed at: 4/25/2024 11:26:49 AM UTC
[2024-04-25T11:26:49.801Z] Executed 'Functions.Function1' (Succeeded, Id=957b8027-bef6-4bc4-af26-438c96c1bef8, Duration=368ms)