Azure Function Queue triggered with Mediatr - DbContext error

azure-functions azure-functions-runtime dbcontext entity-framework-core mediatr

Question

I'm implementing Queue triggered azure function - I'm using a Mediator Pattern library called Mediatr for enhancing command query segregation - and using the latest run-time (2.0.12382.0) constructor dependency injection in Azure Function according to the following tutorial https://devkimchi.com/2019/02/22/performing-constructor-injections-on-azure-functions-v2/

For each Azure function trigger, I call a Mediatr CommandHandler but i'm receiving error :

"A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations."

The error states that i'm trying to access the same instance of DbContext from parallel tasks. however I only have one command handler (Mediatr Handler) and one Query Handler. and i'm using constructor injection for that

I tried to change the Meditr service to be transient in the startup , but still receive the same error on testing the function inside the azure function emulator

Startup Class

public class StartUp : IWebJobsStartup
{

    public void Configure(IWebJobsBuilder builder)
    {
        var configuration = new ConfigurationBuilder()
             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
             .AddEnvironmentVariables()
             .Build();
        var connection = configuration.GetConnectionString("Default"); 
        builder.Services.AddDbContext<CoreDBContext>(options =>
        {
            options.UseSqlServer(connection, p =>
            {
                p.MigrationsAssembly("B12Core.Persistence");
            });
        }
        );
        builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
        builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehaviour<,>));
        builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
        builder.Services.AddMediatR(p =>
        {
            p.AsTransient();

        }, typeof(CreateMessageCommand).GetTypeInfo().Assembly);
    }
}

Full Error

System.Private.CoreLib: Exception while executing function: Function1. Microsoft.EntityFrameworkCore: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.

1
0
3/31/2019 12:53:52 PM

Popular Answer

Solved it by changing the db context injection lifetime to ServiceLifetime.Transient

 builder.Services.AddDbContext<CoreDBContext>(options =>
            {
                options.UseSqlServer(connection, p =>
                {
                   p.MigrationsAssembly("Presistence");
                });
            },ServiceLifetime.Transient
            );
0
3/31/2019 3:03:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow