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

unitOfWork.Complete been canceled by UserFriendlyException #1772


User avatar
0
moustafa created

Hello, in my service i have condition that if true then must make some changes to database, but because i'm using UserFriendlyException it rollback all changes made, so i tried unitOfWork.Complete() but unfortunately the same behaviour

///////////////////////////////////
/////////// rest of code  /////////
///////////////////////////////////
                      if (condition)
                        {
                            
                            using (var unitOfWork = _unitOfWorkManager.Begin())
                            {
                               patient.IsActive = false;
                             _patientRepository.Update(patient);
                                unitOfWork.Complete();

                            }
                            throw new UserFriendlyException(18, L("CannotBookAppointmentIsAgeRestriction"), "");
                        }

/////////// another conditions /////////
///////////////////////////////////
/////////// rest of code  /////////
///////////////////////////////////

4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    UserFriendlyException is an exception and it rollbacks the transaction. this is by design. I suppose you want that this should work even other changes are rolledback:

    using (var unitOfWork = _unitOfWorkManager.Begin())
                                {
                                   patient.IsActive = false;
                                 _patientRepository.Update(patient);
                                    unitOfWork.Complete();
    
                                }
    

    If so, use _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew) which will create an isolated UOW.

  • User Avatar
    0
    moustafa created

    actually i tried as you mention

    using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
                                {
                                    patient.IsActive = false;
                                    _patientRepository.Update(patient);
                                    unitOfWork.Complete();
                                }
    

    but i got this error

    nHandling.AbpApiExceptionFilterAttribute - An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
    System.InvalidOperationException: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
       at System.Data.Entity.Core.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
       at System.Data.Entity.Core.Objects.ObjectContext.AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet)
       at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
       at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassa.<Attach>b__9()
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
       at System.Data.Entity.DbSet`1.Attach(TEntity entity)
       at Abp.EntityFramework.Repositories.EfRepositoryBase`3.AttachIfNot(TEntity entity) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Repositories\EfRepositoryBaseOfTEntityAndTPrimaryKey.cs:line 208
       at Castle.Proxies.PatientRepositoryProxy.AttachIfNot_callback(Patient entity)
       at Castle.Proxies.Invocations.EfRepositoryBase`3_AttachIfNot_26.InvokeMethodOnTarget()
       at Castle.DynamicProxy.AbstractInvocation.Proceed()
       at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 53
       at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 45
       at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35
       at Castle.DynamicProxy.AbstractInvocation.Proceed()
       at Castle.Proxies.PatientRepositoryProxy.AttachIfNot(Patient entity)
       at Abp.EntityFramework.Repositories.EfRepositoryBase`3.Update(TEntity entity) in D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Repositories\EfRepositoryBaseOfTEntityAndTPrimaryKey.cs:line 151
       at Castle.Proxies.PatientRepositoryProxy.Update_callback(Patient entity)
       at Castle.Proxies.Invocations.EfRepositoryBase`3_Update_26.InvokeMethodOnTarget()
       at Castle.DynamicProxy.AbstractInvocation.Proceed()
       at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 53
       at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 45
       at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35
       at Castle.DynamicProxy.AbstractInvocation.Proceed()
       at Castle.Proxies.PatientRepositoryProxy.Update(Patient entity)
    

    far away of this problem i always got this warning Abp.Logging.LogHelper - Can not find 'InternalServerError' in localization source 'AbpWeb'!

    although i extended AbpWeb sources as explained here but it doesn't work [http://www.aspnetboilerplate.com/Pages/Documents/Localization#DocExtending])

    thank you

  • User Avatar
    0
    hikalkan created
    Support Team

    An entity can not be shared by multiple dbcontext and multiple transaction. This is an exception of Entity Framework. In the second UOW, get entity from repository and then update it.

    For the second problem, please provide additional info (like how you extended it), because this works for us.

  • User Avatar
    0
    moustafa created

    thank you very much for this information i didn't know it before actually it fixed my problem

    for the second problem actually i'm sorry it was my mistake it work now just fine i had a spelling mistake in name of dictionary provider AbpWeb :D i appreciate your help