DBContext依赖项注入不适用于Azure计时器功能

时间:2020-10-20 19:03:16

标签: c# azure entity-framework dependency-injection azure-functions

我正在使用EF Core上下文访问数据库。因此,我正在按照以下步骤在azure函数启动中注册DbContext

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

在HttpTrigger的情况下,这可以正常工作。而且我可以从表中查询。但是在使用定时器功能的情况下,通过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)

奇怪的是,此错误仅在使用计时器功能的情况下发生。

问题可能出在哪里?

感谢您的帮助!

0 个答案:

没有答案