3

I'm playing with WebJobs SDK v3.0.5, using a very simple .NET Core 2.2 Console project as follows:

TimerHost.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <LangVersion>7.1</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.5" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.2" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
  </ItemGroup>

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

Program.cs

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace TimerHost
{
    public class Program
    {
        public static async Task Main()
        {
            var builder = new HostBuilder();
            var host = builder
                .UseEnvironment("Development")
                .ConfigureServices((context, services) =>
                {
                    services.AddSingleton(context.Configuration);
                })
                .ConfigureWebJobs(webJobsBuilder =>
                {
                    webJobsBuilder
                        .AddAzureStorageCoreServices()
                        .AddTimers();
                })
                .ConfigureLogging((context, b) =>
                {
                    b.SetMinimumLevel(LogLevel.Debug);
                    b.AddConsole();
                })
                .UseConsoleLifetime()
                .Build();

            await host.RunAsync();
        }
    }

    public static class Function
    {
        public static void Run([TimerTrigger("*/10 * * * * *")] TimerInfo timer, ILogger logger)
        {
            logger.LogInformation($"Running job for timer. Next 3 runs are: {timer.FormatNextOccurrences(3)}");
        }
    }
}

appsettings.json

{
}

The trigger runs fine. However, according to the latest docs (https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#multiple-instances), the timer should run implicitly as a singleton, which means it should be using an Azure storage account for distributed locking support.

When using Azure Functions locally, I would expect to provide a setting like this:

{
  "AzureWebJobsStorage": "UseDevelopmentStorage=true"
}

Otherwise I actually can't run a function, I get an error saying this setting is required, however in the Console host example, I don't get any error at all.

Can someone explain why the console host is not requiring the use of a default storage account? How is the timer maintaining singleton behavior in this scenario?

Sam
  • 6,167
  • 30
  • 39

1 Answers1

3

I spent some time debugging the WebJobs SDK source code from my app, and found more information on what is happening under the hood:

  • If the AzureWebJobsStorage app setting is not defined in the configuration, then the SDK falls back to using an in-memory distributed lock manager for timer and singleton triggers. There is no logging associated with this fallback, and the default lock manager is suitable for local development only.

  • Azure Storage Emulator can be used instead by setting the connection string as you would for Azure Functions, just make sure you've re-built your project so that the appsettings.json files are propagated to the project output folder, this tripped me up for a bit.

  • No errors or log entries are emitted if the value of AzureWebJobsStorage is not a valid local or cloud-based storage account connection string - the configuration will just silently fall back to the in-memory lock manager instead.

Sam
  • 6,167
  • 30
  • 39