DBContext dependency injection not working for azure timer function

Viewed 304

I'm using EF Core context to access a database. So I'm registering DbContext in azure function startup as follow

    public class Startup : FunctionsStartup
    {
      public override void Configure(IFunctionsHostBuilder builder)
      {
          builder.Services.AddDbContext<DataContext>(
            options => options.UseSqlServer(sqlConnectionString));
      }
    }

This is working fine in the case of HttpTrigger. And I'm able to query from the table. But in the case of the timer function always getting the following error while accessing the DB through EF

    Exception: Cannot access a disposed object. A common cause of this error is disposing a context 
    that was resolved from dependency injection and then later trying to use the same context 
    instance elsewhere in your application. This may occur if you are calling Dispose() on the 
    context, or wrapping the context in a using statement. If you are using dependency injection, you 
    should let the dependency injection container take care of disposing context instances.


   Stack :  at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)

Strange thing is, This error only happening in case of the timer function.

What was the issue might be?

Any help is appreciated!.

0 Answers
Related