.NET 5.0 Isolated Azure Function not firing code - Timing Out

Viewed 951

Referencing these instructions:

Creating-Functions

Function-Using-Net-5

Isolated-Functions

Official-2

I've created a .NET 5.0 isolated function, all appears ok initially. When I run it, it just times out:

2021-04-01T12:16:43.912 [Information] Executing 'Functions.MediaUploadProcessorFunction' (Reason='New blob detected: uploads/32~Flowers.png', Id=729ad0ad-0668-4d88-84de-5c623f8a01c1)
2021-04-01T12:16:44.068 [Information] Trigger Details: MessageId: 759690bc-4ca7-4007-8084-ff1bf01de571, DequeueCount: 1, InsertionTime: 2021-04-01T12:16:42.000+00:00, BlobCreated: 2021-04-01T12:16:34.000+00:00, BlobLastModified: 2021-04-01T12:16:34.000+00:00
2021-04-01T12:21:19.917 [Information] Host Status: {"id": "myfunctionsdev","state": "Running","version": "3.0.15405.0","versionDetails": "3.0.15405 Commit hash: c696322564f1f9dc9557bfa495c0485ddf71eeef","platformVersion": "92.0.7.77","instanceId": "05815e0557966c201dc16275542526907206bb509874c0c62f71bc49fbcaa301","computerName": "RD281878F6217D","processUptime": 327992}275542526907206bb509874c0c62f71bc49fbcaa301","computerName": "RD281878F6217D","processUptime": 327992}tion' (Id: '729ad0ad-0668-4d88-84de-5c623f8a01c1'). Initiating cancellation.'Functions.MediaUploadProcessorFunction' (Id: '729ad0ad-0668-4d88-84de-5c623f8a01c1'). Initiating cancellation.4de-5c623f8a01c1, Duration=301517ms)Timeout value of 00:05:00 was exceeded by function: Functions.MediaUploadProcessorFunction, Duration=301517ms)Timeout value of 00:05:00 was exceeded by function: Functions.MediaUploadProcessorFunction
2021-04-01T12:21:46.469 [Warning] A function timeout has occurred. Restarting worker process executing invocationId '729ad0ad-0668-4d88-84de-5c623f8a01c1'.
2021-04-01T12:21:46.472 [Information] Restarting channel '6c9e4a92-c2d5-48c4-9474-7d3e70f7a6d4' that is executing invocation '729ad0ad-0668-4d88-84de-5c623f8

It detects the function and the trigger fires, but the actual function does not run (the debugger does NOT attach).

[Function(nameof(MediaUploadProcessorFunction))]
public async Task Run([BlobTrigger(containerName + "/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log)
{
    System.Diagnostics.Debugger.Launch();
    System.Diagnostics.Debugger.Break();

    log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}

public MediaUploadProcessorFunction(IEmailService emailService, IMediaRepository mediaRepository, IOptions<ErrorEmailOptions> errorEmailOptions, IOptions<ValuesOptions> valuesOptions, IGroupRepository groupRepository)
{
    System.Diagnostics.Debugger.Launch();
    System.Diagnostics.Debugger.Break();

    _emailService = emailService ?? throw new ArgumentNullException(nameof(emailService));
    _mediaRepository = mediaRepository ?? throw new ArgumentNullException(nameof(mediaRepository));
    _errorEmailOptions = errorEmailOptions?.Value ?? throw new ArgumentNullException(nameof(errorEmailOptions));
    _values = valuesOptions?.Value ?? throw new ArgumentNullException(nameof(valuesOptions));
    _groupRepository = groupRepository ?? throw new ArgumentNullException(nameof(groupRepository));
}

and the start up (the debugger does attach OK):

static Task Main(string[] args)
{
    System.Diagnostics.Debugger.Launch();

    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

    var host = new HostBuilder()
        .ConfigureAppConfiguration(configurationBuilder =>
        {
            configurationBuilder.AddCommandLine(args);
        })
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(services =>
        {
            services.AddLogging();

            services.Configure<ValuesOptions>(config.GetSection("Values"));
            services.AddScoped<IMediaRepository, MediaRepository>();
            services.Configure<ErrorEmailOptions>(config.GetSection("ErrorEmail"));
            services.AddScoped<IEmailService, EmailService>();
            services.AddScoped<IGroupRepository, GroupRepository>();

            services.AddDbContext<Context>(opts =>
            {
                var conn = config.GetConnectionString("Context");
                opts.UseSqlServer(conn);
            });
        })
        .Build();

    return host.RunAsync();
}

Anybody have any ideas I can try?

1 Answers

Out of process means your function host and your runtime are running in different processes, so you have to attach the debugger to the process for you to debug.

  1. In Visual Studio, run your function with this argument set on the properties of the function project's csproj that holds the function (it's handy to do this so you don't have to repeat the command in future runs):

host start --dotnet-isolated-debug --verbose

You can do this in VS by doing right click on the project > Properties > Debug > Application Arguments, and paste this command there (alternatively, in the command line, cd into the function project and run the same command plus the keyword func in the beginning of the command).

  1. Run the function project as the startup project. Right click the function project > Set as Startup. Then use f5 or press Start

  2. Next you wait for the function to run and display the Process ID (PID) in the Function's console. Copy that.

  3. Go back to VS, tap Ctrl + Alt + P to attach to the process (search for dotnet to filter out other machine processes)

Immediately after this you will see the function react saying it attached to a process. Now you can debug. This should stop the function from timing out on your local machine and stop at any breakpoint you need in the execution path of the function. Cheers =)

Related