The listener for function 'Functions.SendNotificationTimer' was unable to start. when running .NET 8 Azure timer function on MacOS locally

60 views Asked by At

I have a .NET 8 Azure timer function running in isolation mode and trying to debug it locally on my Mac, but keep getting errors when running it. I also have a http trigger function in the same project and it works. I'm seeing both failed to acquire host lock lease errors and The listener for function 'Functions.SendNotificationTimer' was unable to start errors.

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

Here is the code, but I don't think this is the problem as I also have an http trigger function in this solution as well and it works.

I saw this post, The listener for function 'Function1' was unable to start when running a timer function, but it was running fine locally.

This post, The listener for function 'Functions.trigger' was unable to start? while running Azure function locally, but I'm using Azurite on a Mac. A connection error does flash up at some point, as one of the answers mentions.

enter image description here

I've followed Code and test Azure Functions locally and Use the Azurite emulator for local Azure Storage development for Azurite and Quickstart: Create a C# function in Azure using Visual Studio Code.

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

There are 1 answers

2
Pravallika KV On BEST ANSWER

This error occurs if there is already an instance of that particular host is running, or if the host process is not shutting down properly.

  • Find the currently running process using PORT 10000 and kill that process using its Process ID. Refer Article using below commands.
lsof -i tcp:<PORT_Number>
kill -15 <Process_ID>
  • Stop and restart the Azurite manually.
  • Or use Azure Storage connection string in the local.settings.json:
{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "<Storage_connection_string>",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
    }
}

I have created a Function project with both Http trigger and Timer Trigger and working as expected.

Program.cs:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();
host.Run();

Function.cs:

public class SendTimerNotification
{
    private readonly ILogger _logger;

    public SendTimerNotification(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<SendTimerNotification>();
    }

    [Function("SendTimerNotification")]
    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}");
        }
    }
}

Console Output:

[2024-03-11T05:51:41.365Z] Found C:\Users\uname\Source\Repos\FunctionApp9\FunctionApp9.csproj. Using for user secrets file configuration.
[2024-03-11T05:51:51.716Z] Azure Functions .NET Worker (PID: 9980) initialized in debug mode. Waiting for debugger to attach...
[2024-03-11T05:51:51.803Z] Worker process started and initialized.

Functions:

        Function1: [GET,POST] http://localhost:7199/api/Function1

        SendTimerNotification: timerTrigger

For detailed output, run func with --verbose flag.
[2024-03-11T05:51:52.267Z] Executing 'Functions.SendTimerNotification' (Reason='Timer fired at 2024-03-11T11:21:52.2282717+05:30', Id=5775589a-c237-4787-8f06-ec863c76c770)
[2024-03-11T05:51:52.272Z] Trigger Details: UnscheduledInvocationReason: IsPastDue, OriginalSchedule: 2024-03-11T09:25:00.0000000+05:30
[2024-03-11T05:51:52.452Z] Next timer schedule at: 3/11/2024 9:25:00 AM
[2024-03-11T05:51:52.452Z] C# Timer trigger function executed at: 3/11/2024 11:21:52 AM
[2024-03-11T05:51:52.486Z] Executed 'Functions.SendTimerNotification' (Succeeded, Id=5775589a-c237-4787-8f06-ec863c76c770, Duration=245ms)
[2024-03-11T05:51:56.703Z] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=cdc9e38b-288e-49b4-8fa5-aed0973f803a)
[2024-03-11T05:51:56.751Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-03-11T05:51:57.062Z] C# HTTP trigger function processed a request.
[2024-03-11T05:51:57.076Z] Executing OkObjectResult, writing value of type 'System.String'.
[2024-03-11T05:51:57.156Z] Executed 'Functions.Function1' (Succeeded, Id=cdc9e38b-288e-49b4-8fa5-aed0973f803a, Duration=464ms)