0

When I start my page everything loads fine I try to reload for the second time I get the following error I run the urls from angular

A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

This is the method

    private readonly PuntovService  puntovService = new PuntovService(new PuntovRepository(CentralContext.Create()));
    private readonly IMapper mapper;

    public PuntovController()
    {
        this.mapper = WebApiApplication.MapperConfiguration.CreateMapper();
    }


    [HttpGet]
    [Route("Puntos")]
    [ResponseType(typeof(IEnumerable<PuntovDTO>))]
    public async Task<IHttpActionResult> GetAll()
    {
        var pv = await puntovService.GetAll();
        var puntovDTO = pv.Select(x =>  mapper.Map<PuntovDTO>(x));
        var tipoComitePersona = from com in puntovDTO
                                select  new {com.PuntoV};
        return Ok(tipoComitePersona);
    }

in this class call the context that would be the database

using APICentralBL.Models;
using System.Data.Entity;

namespace APICentralBL.Data
{

    public  class CentralContext : DbContext
    {

        private static CentralContext centralContext = null;
        public CentralContext() : base("Context") 
        {
        
        }
        public DbSet<SDN> SDN {  get; set;}
        public DbSet<CALIF> CALIF { get; set; }
        public DbSet<UVT> UVT { get; set; }
        public DbSet<PUNTOV> PUNTOV { get; set; }
        public DbSet<TRANS> TRANS { get; set; }

        public static CentralContext Create()
        {
            if (centralContext == null)
                centralContext = new CentralContext();
              return centralContext;
        }
    }
}

It not only happens with this method, it happens when I try to call more than one

ex.StackTrace

   en System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered()
   en System.Data.Entity.Core.Objects.ObjectQuery`1.System.Data.Entity.Infrastructure.IDbAsyncEnumerable<T>.GetAsyncEnumerator()
   en System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ForEachAsync[T](IDbAsyncEnumerable`1 source, Action`1 action, CancellationToken cancellationToken)
   en System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ToListAsync[T](IDbAsyncEnumerable`1 source, CancellationToken cancellationToken)
   en System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.ToListAsync[T](IDbAsyncEnumerable`1 source)
   en APICentralBL.Repositories.Implements.GenericRepository`1.<GetAll>d__3.MoveNext() en D:\ANTIOQUENA DEVELOPERS REPOSITORY\ANTIOQUENA DEVELOPERS\DEVELOPMENTS\API'S PROJECT\APICentral-Deploy\APICentralBL\Repositories\Implements\GenericRepository.cs: línea 33
   en System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   en System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   en APICentralBL.Services.Implements.GenericService`1.<GetAll>d__3.MoveNext() en D:\ANTIOQUENA DEVELOPERS REPOSITORY\ANTIOQUENA DEVELOPERS\DEVELOPMENTS\API'S PROJECT\APICentral-Deploy\APICentralBL\Services\Implements\GenericService.cs: línea 25
   en System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   en System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   en APICentral.Controllers.PuntovController.<GetAll>d__4.MoveNext() en D:\ANTIOQUENA DEVELOPERS REPOSITORY\ANTIOQUENA DEVELOPERS\DEVELOPMENTS\API'S PROJECT\APICentral-Deploy\APICentral\Controllers\PuntovController.cs: línea 65**
Hackerman
  • 117
  • 5
  • You might want to check other questions like https://stackoverflow.com/questions/48767910/entity-framework-core-a-second-operation-started-on-this-context-before-a-previ – Progman Jul 09 '22 at 19:23
  • Please [edit] your question to include your source code as a working [mcve], which can be compiled and tested by others. – Progman Jul 09 '22 at 19:24
  • Reproducible How can I reproduce all of this from here? I don't see that there is a way – Hackerman Jul 09 '22 at 19:30
  • 2
    What gave you the idea to put a static `centralContext` instance inside of the DbContext class?!?!? It's weird and wrong. Just follow the Microsoft documentation. – JHBonarius Jul 09 '22 at 19:53

1 Answers1

3

Since your context is stored in a static field, it will be shared by all threads serving requests to your controllers concurrently. You should usually use a context per request

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46