multithreading - 控制台应用程序 - DbContext 实例不能在 OnConfiguring 内部使用

标签 multithreading dependency-injection .net-core console-application ef-core-2.0

我正在使用 Asp.Net Core 控制台应用程序和 Entiy Framework Core 以及工作单元存储库模式。当我使用多线程函数时,出现此错误:

DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point. This can happen if a second operation is started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.

UnitOfwork.cs

public interface IUnitOfWork : IDisposable
{
    void Commit();
    ApplicationDbContext GetContext();
}

public class UnitOfWork : IUnitOfWork
{
    private readonly ApplicationDbContext _applicationDbContext;

    public UnitOfWork(ApplicationDbContext applicationDbContext)
    {
        _applicationDbContext = applicationDbContext;
    }

    public void Commit()
    {
        try
        {
            _applicationDbContext.SaveChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    public ApplicationDbContext GetContext()
    {
        return _applicationDbContext;
    }

    public void Dispose()
    {
        _applicationDbContext.Dispose();
    }
}

IRepository.cs

public interface IGenericRepository<T>
    where T : class, IEntity
{
    List<T> GetAll(Expression<Func<T, bool>> filter = null,
        Func<IQueryable<T>, IOrderedEnumerable<T>> orderBy = null,
        string includeProperties = "");

    T FindSingle(int id);

    T FindBy(Expression<Func<T, bool>> predicate, string includeProperties = "");

    void Add(T toAdd);

    void Update(T toUpdate);

    void Delete(int id);

    void Delete(T entity);
}

存储库.cs

public class GenericRepository<T> : IGenericRepository<T>
    where T : class, IEntity
{
    private readonly IUnitOfWork _unitOfWork;

    public GenericRepository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public virtual List<T> GetAll(Expression<Func<T, bool>> filter = null,
        Func<IQueryable<T>, IOrderedEnumerable<T>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<T> query = _unitOfWork.GetContext().Set<T>();

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (string includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }

        return query.ToList();
    }

    public virtual T FindSingle(int id)
    {
        return _unitOfWork.GetContext().Set<T>().Find(id);
    }

    public virtual T FindBy(Expression<Func<T, bool>> predicate, string includeProperties = "")
    {
        IQueryable<T> query = _unitOfWork.GetContext().Set<T>();
        foreach (string includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }
        return query.Where(predicate).FirstOrDefault();
    }

    public virtual void Add(T toAdd)
    {
        _unitOfWork.GetContext().Set<T>().Add(toAdd);
    }

    public virtual void Update(T toUpdate)
    {
        _unitOfWork.GetContext().Entry(toUpdate).State = EntityState.Modified;
    }

    public virtual void Delete(int id)
    {
        T entity = FindSingle(id);
        _unitOfWork.GetContext().Set<T>().Remove(entity);
    }

    public virtual void Delete(T entity)
    {
        _unitOfWork.GetContext().Set<T>().Remove(entity);
    }
}

商业服务;

public interface IUserService
{
    void CreateUser(UserEntity userEntity, bool commit = false);
}
public class UserService : IUserService
{
    private readonly IGenericRepository<UserEntity> _userRepository;
    private readonly IUnitOfWork _unitOfWork;

    public UserService(IUnitOfWork unitOfWork, IGenericRepository<UserEntity> userRepository)
    {
        _unitOfWork = unitOfWork;
        _userRepository = userRepository;
    }

    public void CreateUser(UserEntity userEntity, bool commit = false)
    {
        try
        {
            _userRepository.Add(userEntity);

            if (commit)
                _unitOfWork.Commit();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

控制台Main.cs;

class Program
{
    public static ServiceProvider ServiceProvider;

    static void Main(string[] args)
    {
        InitializeIoc();

        Task.Run(() => { FuncA(); });

        Task.Run(() => { FuncB(); });

        Console.ReadLine();
    }

    private static void InitializeIoc()
    {
        ServiceProvider = new ServiceCollection()
            .AddDbContext<ApplicationDbContext>()
            .AddTransient<IUnitOfWork, UnitOfWork>()
            .AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>))
            .AddTransient<IUserService, UserService>()
            .BuildServiceProvider();
    }

    private static void FuncA()
    {
        var userService = ServiceProvider.GetService<IUserService>();
        for (int i = 0; i < 100; i++)
        {
            userService.CreateUser(new UserEntity { FirstName = "FuncA_" + Guid.NewGuid(), LastName = "Last", CreatedDate = DateTime.Now }, false);
        }
    }
    private static void FuncB()
    {
        var userService = ServiceProvider.GetService<IUserService>();
        for (int i = 0; i < 100; i++)
        {
            userService.CreateUser(new UserEntity { FirstName = "FuncB_" + Guid.NewGuid(), LastName = "Last", CreatedDate = DateTime.Now }, false);
        }
    }
}

如何解决这个问题?

感谢您的帮助。

最佳答案

问题是使用的 AddDbContext 将您的 ApplicationDbContext 注册到 ServiceLifetime.Scoped,但您没有创建范围,因此它有效地工作为单例,因此由多个线程同时共享和访问,这会导致相关异常(并且可能会导致许多其他异常,因为 DbContext 不是线程安全的)。

解决方案是使用范围,例如调用CreateScope并使用返回的对象ServiceProvider属性来解析服务:

private static void FuncA()
{
    using (var scope = ServiceProvider.CreateScope())
    {
        var userService = scope.ServiceProvider.GetService<IUserService>();
        // Do something ...
    }
}

private static void FuncB()
{
    using (var scope = ServiceProvider.CreateScope())
    {
        var userService = scope.ServiceProvider.GetService<IUserService>();
        // Do something ...
    }
}

关于multithreading - 控制台应用程序 - DbContext 实例不能在 OnConfiguring 内部使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52603280/

相关文章:

c# - 为什么我会收到此错误 :"Cross-thread operation not valid: Control lbFolders accessed from a thread other than the thread it was created on."?

windows - WaitForMultipleObjects() 是否重置所有自动重置事件?

java - 我如何理解 Thread.interrupt()?

android - 在 Android 上使用 Dagger 2 进行延迟注入(inject)

c# - 如何在我的服务中获取 IHostApplicationLifetime 并将其注入(inject)容器(控制台应用程序)

nuget - dotnet core 是否有命令行包安装程序

c# - Parallel.ForEach 中长时间运行进程的进度条

java - 服务定位器与依赖注入(inject)

asp.net-core - 如何向 ASP.NET Core Identity 添加更多字段?

c# - IsWithinDistance 无法翻译