return View in .net core show that (IFeatureCollection has been disposed. Object name: 'Collection')

44 views Asked by At

Somebody help me please.
I'm using .net core 7.
My register process works well. But I'm not able to return View.

I use the Unit of Work pattern in Data Access Layer.

Here is my IUnitOfWork.cs (from data access layer)

using JS.Dal.Interfaces;

namespace JS.Dal
{
    public interface IUnitOfWork : IDisposable
    {
        void BeginTransaction();
        void BeginTransactionAsync();
        void SaveChanges();
        void SaveChangesAsync();

        IUserRepository User { get; }
        //***some Repositories are removed for simplicity***
    }
}

Here is my UnitOfWork.cs (from data access layer)

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using JS.Dal.Interfaces;
using JS.Dal.Repositories;

namespace JS.Dal
{
public class UnitOfWork : IUnitOfWork , IDisposable
{
    private readonly ApplicationDBContext _dbContext;
    private IDbContextTransaction _transaction;
    public UnitOfWork(ApplicationDBContext dbContext)
    {
    _dbContext = dbContext;
    Dispose(false);
    }

    private IUserRepository _user;
    //***some Repositories are removed for simplicity***
    
    public IUserRepository User
    {
    get
    {
            if (_user == null)
            {
                _user = new UserRepository(_dbContext);
        }
        return _user;
    }
    }

    public void SaveChanges()
    {
    _dbContext.SaveChanges();
    }
    public async void SaveChangesAsync()
    {
    await _dbContext.SaveChangesAsync();
    }
    public void BeginTransaction()
    {
    _transaction = _dbContext.Database.BeginTransaction();
    }
    public async void BeginTransactionAsync()
    {
    _transaction = await _dbContext.Database.BeginTransactionAsync();
    }
    public void Rollback()
    {
    foreach (var entry in _dbContext.ChangeTracker.Entries())
    {
            switch (entry.State)
        {
            case EntityState.Added:
            entry.State = EntityState.Detached;
            break;
        }
    }
    }
    public IRepository<T> Repository<T>() where T : class
    {
    return new Repository<T>(_dbContext);
    }

    private bool disposed = false;
    protected virtual void Dispose(bool disposing)
    {
    if (!this.disposed)
    {
        if (disposing)
        {
                _dbContext.Dispose();
        }
    }
    this.disposed = true;
    }
    public void Dispose()
    {
    Dispose(true);
    GC.SuppressFinalize(this);
    }
}
}

Here is my Controller (from presentation layer)

[HttpPost]
public async Task<IActionResult> DoRegister(RegisterDto dtoRegister, String viewToReturn)
{
    if (ModelState.IsValid)
    {
    var json = JsonConvert.SerializeObject(dtoRegister);
    StringContent contentData = new StringContent(
                                        json, Encoding.UTF8, "application/json");
    Uri uri = new Uri($"http://localhost:54302/api/Security/Account/Register/");

    var response = await _httpClient.Client.PostAsync(uri, contentData);

    if (response.IsSuccessStatusCode)
    {
            var result = response.Content.ReadAsStringAsync().Result;
        ViewBag.msg = result;
        return View("Login");  // <= ***My problem is on this line***
    }
    else
    {
        ViewBag.msg = response.ReasonPhrase;
        return View(viewToReturn.ToString());
    }
    }
    return BadRequest();
}

Below code from above Controller show that " IFeatureCollection has been disposed. Object name: 'Collection' "

return View("Login")

I would really appreciate it if someone could assist me with this matter.

0

There are 0 answers