Blazor服务器和EF核心:在前一个操作完成之前,在此上下文实例上启动了第二个操作 [英] Blazor Server and EF Core: A second operation was started on this context instance before a previous operation completed

查看:0
本文介绍了Blazor服务器和EF核心:在前一个操作完成之前,在此上下文实例上启动了第二个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ef core有问题。我有两个从数据库读取数据的服务。在一个页面上是调用第一服务,在第二页面上是第二服务。当我点击按钮来创建一个新程序时,我得到了错误。我通常从带有注入服务的页面调用它。有人能帮我吗?

Show in application

builder.Services.AddDbContextPool<Context>(options =>
{ 
options.UseSqlServer(builder.Configuration.GetConnectionString("Connection"));
});

TestService1:

public class TestService1 : ITestService1
{
    private readonly Context _context;
    private readonly IMapper _mapper;

    public TestService1(Context context, IMapper mapper)
    {
        _kreativgangContext = kreativgangContext;
        _mapper = mapper;
    }

    public virtual async Task<AllProgramViewModel> HandleAsync(AllProgramFilterViewModel filter)
    {
        var model = new AllProgramViewModel();

        var data = _context.Programs.Where(x => (EF.Functions.Like(x.Name ?? "", "%" + filter.Name + "%") || string.IsNullOrEmpty(filter.Name)))
            .Select(x => new Core.Models.Program() { ID = x.ID, Name = x.Name, Order = x.Order });

        result.Model.TotalCount = await data.CountAsync();

        result.Model.Items = data.Select(x => _mapper.Map<AllProgramItemViewModel>(x));
    
        return model;
    }
}

public interface ITestService1
{
    public Task<AllProgramViewModel> HandleAsync(AllProgramFilterViewModel filter);
}

测试服务2:

    public class TestService2 : ITestService2
{
    private readonly Context _context;

    public TestService2(Context context)
    {
        _context = context;
    }

    public virtual async Task<NewProgramViewModel> HandleAsync()
    {
        var model = new NewProgramViewModel();

        List<ProgramOrderViewModel> items = _context.Programs.Select(x => new Core.Models.Program() { Order = x.Order, ID = x.ID })
            .Select(x => new ProgramOrderViewModel()
            {
                ID = x.ID,
                Order = x.Order
            }).ToList();

        return await Task.FromResult(model);
    }
}

public interface ITestService2
{
    public Task<NewProgramViewModel> HandleAsync();
}

错误:

Error: System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Mitar.Kreativgang.Admin.Handlers.TestService2.HandleAsync() in D:ProgrammingKreativgangSrcMitar.Kreativgang.AdminHandlersTestService2.cs:line 26
   at Mitar.Kreativgang.Admin.Pages.Program.ProgramNew.OnInitializedAsync() in D:ProgrammingKreativgangSrcMitar.Kreativgang.AdminPagesProgramProgramNew.razor:line 114
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

推荐答案

这是一个已知且有文档记录的陷阱,如ASP.NET Core Blazor Server with Entity Framework Core (EFCore)中所述。在Blazor Server中,DI作用域是用户电路--本质上是用户会话。这意味着像TestService2DbContext这样的scoped服务将在内存中保留很长时间,并最终被多个方法和操作重用。

如文件所述:

Blazor Server是一个有状态应用程序框架。该应用程序保持与服务器的持续连接,用户的状态以电路形式保存在服务器的内存中。用户状态的一个示例是依赖项注入(DI)服务实例中保存的数据,这些服务实例的作用域为电路。Blazor Server提供的唯一应用程序模型需要使用实体框架核心的特殊方法。

您需要注册和使用DbConextFactory(或PooledDbContextFactory)而不是DbConextPool,并在使用它的位置创建一个新的DbContext实例。

builder.Services.AddDbContextFactory<ContactContext>(opt =>
    opt.UseSqlServer(...));

builder.Services.AddPooledDbContextFactory<ContactContext>(opt =>
    opt.UseSqlServer(...));

服务构造函数应接受工厂而不是上下文:

    public TestService2(AddDbContextFactory<ContactContext> factory)
    {
        _factory = factory;
    }

    public virtual async Task<NewProgramViewModel> HandleAsync()
    {
        
        using var context=_factory.CreateContext())
        {
        ...
        }

    }

组件范围

要将DbContext的作用域限制为单个组件,仅注入DbConextFactory是不够的。当用户离开组件时,需要显式释放DbContext实例。为此,组件需要实现IDisposable。这在Scope to the component lifetime

一节中进行了解释
@implements IDisposable
@inject IDbContextFactory<ContactContext> DbFactory
...

@code 
{

    ContactContext? Context;

    public void Dispose()
    {
        Context?.Dispose();
    }

    protected override async Task OnInitializedAsync()
    {
        Context = DbFactory.CreateDbContext();
        ...
    }

}
 

这篇关于Blazor服务器和EF核心:在前一个操作完成之前,在此上下文实例上启动了第二个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆