Open Closed

Problem with Repository #876


0
maddey234 created

Hello,

I am trying to fetch a list of accounts from an accounts table and I keep getting the error that "The operation cannot be completed because the DbContext has been disposed."

I have followed exactly the example in this article"http://aspnetzero.com/Documents/Developing-Step-By-Step-MPA"

This is where I get the error:

public class PersonAppService : PhoneBookAppServiceBase, IPersonAppService
{
    private readonly IRepository<Person> _personRepository;

    public PersonAppService(IRepository<Person> personRepository)
    {
        _personRepository = personRepository;
    }

    public ListResultOutput<PersonListDto> GetPeople(GetPeopleInput input)
    {
        var persons = _personRepository
            .GetAll()
            .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Name.Contains(input.Filter) ||
                        p.Surname.Contains(input.Filter) ||
                        p.EmailAddress.Contains(input.Filter)
            )
            .OrderBy(p => p.Name)
            .ThenBy(p => p.Surname)
            .ToList();

        return new ListResultOutput<PersonListDto>(persons.MapTo<List<PersonListDto>>());
    }
}

while mine is like this:

public class AccountAppService : MyCooperatifAppServiceBase, IAccountAppService
    {

        private readonly IRepository<Account> _accountRepository;

        public AccountAppService(IRepository<Account> accountRepository)
        {
            _accountRepository = accountRepository ;
        }


        public ListResultOutput<AccountListDto> GetAccounts(GetAccountsInput input)
        {
                    
            var accounts =  _accountRepository
                .GetAll()
                .WhereIf(
               !string.IsNullOrWhiteSpace(input.Filter),
                    a => a.Name.Contains(input.Filter) 
                )
                .OrderBy(a => a.Id)
                .ThenBy(a => a.Name)
                .ToList();

            return new ListResultOutput<AccountListDto>(accounts.MapTo<List<AccountListDto>>());
        }
    }
}

My AccountListDto does not contain any foregin keys whatsoever, so I don't know why I keep getting that error. Please help


6 Answer(s)
  • 0
    hikalkan created

    Can you share your exteption's stacktrace and AccountListDto class?

  • 0
    julianrobertshawe created

    I am also getting the same error -

    An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

    Additional information: The operation cannot be completed because the DbContext has been disposed.

    I followed the getting started with MPA documentation

    Can you pass on the fix is possible

    thanks

  • 0
    julianrobertshawe created

    my code

    using System.Web.Mvc; using Abp.Web.Mvc.Authorization; using AusXpress.AusXpress.Web.Controllers; using AusXpress.AusXpress.Authorization.Users; using Abp.Runtime.Session; using AusXpress.AusXpress.Customers.dto; using AusXpress.AusXpress.Customers;

    namespace AusXpress.AusXpress.Web.Areas.Mpa.Controllers { [AbpMvcAuthorize] public class WelcomeController : AusXpressControllerBase { private readonly UserManager _userManager; private readonly CustomerAppService _customerAppService;

        public WelcomeController(
            UserManager userManager,
            CustomerAppService customerAppService)
        {
            _userManager = userManager;
            _customerAppService = customerAppService;
    
        }
    
    
        public ActionResult Index()
        {
            var user =  _userManager.GetUserByIdAsync(AbpSession.GetUserId());
            var customerInput = new GetCustomerInput { UserId = user.Id };
    
    
    
            var customers = _customerAppService.GetCustomer(customerInput);
            return View();
        }
    }
    

    }

    public ListResultOutput

            var customers = _customerRepository
                .GetAll()
                .Include(p => p.Addresses)
                .WhereIf(
                    input.UserId != 0,
                    p => p.UserId.Equals(input.UserId)
                )
                .ToList();
    
            return new ListResultOutput<CustomerListDto>(customers.MapTo<List<CustomerListDto>>());
        }
    
  • 0
    julianrobertshawe created

    [InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.] System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +4284477 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18 System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +53 System.Data.Entity.Internal.Linq.InternalSet1.Include(String path) +18 System.Data.Entity.Infrastructure.DbQuery1.Include(String path) +66 System.Data.Entity.QueryableExtensions.Include(IQueryable1 source, String path) +130 System.Data.Entity.QueryableExtensions.Include(IQueryable1 source, Expression1 path) +181 AusXpress.AusXpress.Customers.CustomerAppService.GetCustomer(GetCustomerInput input) in C:\source\AusXpress\AusXpress.AusXpress.Application\Customer\CustomerAppService.cs:39 AusXpress.AusXpress.Web.Areas.Mpa.Controllers.WelcomeController.Index() in C:\source\AusXpress\AusXpress.AusXpress.Web\Areas\Mpa\Controllers\WelcomeController.cs:34 lambda_method(Closure , ControllerBase , Object[] ) +61 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +30 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +197 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +46 System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +37 System.Web.Mvc.Async.AsyncControllerActionInvoker.

  • 0
    ismcagdas created

    Hi,

    You need to inject your interface not the application class itself. If you want to inject your application service class, then your method must be virtual.

  • 0
    julianrobertshawe created

    Perfect! so simple now you know :)

    thanks