Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Problem with Repository #876


User avatar
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)
  • User Avatar
    0
    hikalkan created
    Support Team

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

  • User Avatar
    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

  • User Avatar
    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<CustomerListDto> GetCustomer(GetCustomerInput input) {

            var customers = _customerRepository
                .GetAll()
                .Include(p => p.Addresses)
                .WhereIf(
                    input.UserId != 0,
                    p => p.UserId.Equals(input.UserId)
                )
                .ToList();
    
            return new ListResultOutput&lt;CustomerListDto&gt;(customers.MapTo&lt;List&lt;CustomerListDto&gt;>());
        }
    
  • User Avatar
    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.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +24 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +43 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +69 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +230 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +42 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +124 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +27 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +32 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +26 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +40 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +24 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +27 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +29 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +23 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9744373 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.

  • User Avatar
    0
    julianrobertshawe created

    Perfect! so simple now you know :)

    thanks