Search code examples
c#.netazure-functionstimer-triggerdotnet-isolated

Azure Function: Did not find functions with language [dotnet-isolated]


I cannot succed in starting my Azure Function. I got the following error:

[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')

My local.settings.json is the following:

{
"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 local emulator is on. My Program.cs in the Azure Function project is:

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();

The functios is:

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);
  }
 }

And the project file .cproj is:

<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>

I can't even figure out what's happening. As I said, azurite local emulator is on, I don't have any problem with it. ChatGPT says that the configuration is correct. I'm trying to troubleshoot, but I cannot find anything clear. When I try to start-up another Azure Function with the same settings and dependencies (I've copied and past dependencies on project file and also the Program.cs and local.settings are similar), I succed. So the problem seem not an hidden wrong settings on my local computer. Any idea?


Solution

    • Delete the bin and obj folders and rebuilding your function project.
    • Rebuild and test the function again.

    I have tested your code with the same settings in my environment, and it worked as expected.

    Move local.settings.json code snippet to ItemGroup of host.json in .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" />
        <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>
    

    Console Response:

    [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)