Starts in:
2 DAYS
4 HRS
29 MIN
0 SEC
Starts in:
2 D
4 H
29 M
0 S
Open Closed

How to Call ApplicationService Method From RabbitMq HandleEventAsync #3170


User avatar
0
shijo created
  • ABP Framework version: v5.2.0

  • UI type: Angular

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): yes

  • Exception message and stack trace:

  • Steps to reproduce the issue:"

    I have implemented RabbitMq in my application, I am getting an exception inside HandleEventAsync when trying to access application service implemented class method. I tried to implement [UnitOfWork], but still, I am getting exceptions.


11 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Can you share the exception message and stack trace and related code?

  • User Avatar
    0
    shijo created

    at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable1 parameters) at Volo.Abp.DependencyInjection.AbpLazyServiceProvider.<>c__DisplayClass12_0.<LazyGetService>b__0() at Volo.Abp.DependencyInjection.AbpLazyServiceProvider.LazyGetService(Type serviceType) at Volo.Abp.DependencyInjection.AbpLazyServiceProvider.LazyGetService(Type serviceType, Object defaultValue) at Volo.Abp.DependencyInjection.AbpLazyServiceProvider.LazyGetService[T](T defaultValue) at Volo.Abp.Application.Services.ApplicationService.get_GuidGenerator() at Akadimi.Books.BookAppDistEventService.<CreateAsync>d__8.MoveNext() in D:\Akadimi\Akadimi\aspnet-core\src\Akadimi.Application\Books\BookAppDistEventService.cs:line 79 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Akadimi.Books.BookAppDistEventService.<>c__DisplayClass7_0.<

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Sorry, It's not enough to reproduce the problem.

    Can you please share a project with me? shiwei.liang@volosoft.com. It will help us solve this problem quickly. thanks.

  • User Avatar
    0
    shijo created
        public class BooksAppService: ApplicationService, IBooksAppService, IDistributedEventHandler<APIBookEto>
        {
            private readonly BookManager _bookManager;
            public BooksAppService(BookManager bookManager)
            {
                _bookRepository = bookRepository;
                _bookManager = bookManager;
            }
            public virtual async Task<BookDto> CreateAsync(BookCreateDto input)
            {
                var titles = ObjectMapper.Map<List<BookTranslationCreateDto>, List<BookTranslation>>(input.inputTitles);
                var book = await _bookManager.CreateAsync(
                input.NoOfPages, input.Dimensions, input.ISBN10, input.ISBN13, input.Price, input.PublishDate,
                titles, input.BookLanguageId, input.BookMediaTypeId, input.AuthorIds, input.PublisherIds, input.TagIds, input.FileId
                );
                return ObjectMapper.Map<Book, BookDto>(book);
            }
           [UnitOfWork]
            public virtual Task HandleEventAsync(APIBookEto eventData)
            {
                Task.Run(async () =>
                {
                    try
                    {
                        var input = ObjectMapper.Map<APIBookEto, BookCreateDto>(eventData);
                        var titles = ObjectMapper.Map<List<BookTranslationCreateDto>, List<BookTranslation>>(input.inputTitles);
                var book = await _bookManager.CreateAsync(
                input.NoOfPages, input.Dimensions, input.ISBN10, input.ISBN13, input.Price, input.PublishDate,
                titles, input.BookLanguageId, input.BookMediaTypeId, input.AuthorIds, input.PublisherIds, input.TagIds, input.FileId
                );
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                });
                return Task.CompletedTask;
            }
        }
    

    I can explain the scenario. Here above you can see inside BooksAppService class I have a CreateAsync which is basically inserts data into the database. I have already written all logic inside _bookManager.CreateAsync() function. As you can see I am listening distributed event HandleEventAsync, inside this event function I am executing the same logic which is written in CreateAsync. CreateAsync method is calling through API controller which is perfectly working fine and HandleEventAsync(this is already marked as [UnitOfWork]) is calling through RabbitMq and is throwing the above exceptions.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Can you try this?

    [UnitOfWork]
    public virtual Task HandleEventAsync(APIBookEto eventData)
    {
        try
        {
            var input = ObjectMapper.Map(eventData);
            var titles = ObjectMapper.Map, List>(input.inputTitles);
            var book = await _bookManager.CreateAsync(
            input.NoOfPages, input.Dimensions, input.ISBN10, input.ISBN13, input.Price, input.PublishDate,
            titles, input.BookLanguageId, input.BookMediaTypeId, input.AuthorIds, input.PublisherIds, input.TagIds, input.FileId);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
    
  • User Avatar
    0
    shijo created

    Yes, It is passing through but the exception is thrown again where DBContext is accessing.

    at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed() at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityType() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.CheckState() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.Where[TSource](IQueryable1 source, Expression1 predicate) at Akadimi.Books.BookManager.<SetBookAuthorsAsync>d__9.MoveNext() in D:\Akadimi\Akadimi\aspnet-core\src\Akadimi.Domain\Books\BookManager.cs:line 123 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Akadimi.Books.BookManager.<CreateAsync>d__7.MoveNext() in D:\Akadimi\Akadimi\aspnet-core\src\Akadimi.Domain\Books\BookManager.cs:line 63 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Akadimi.Books.BooksAppService.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    This is my test code and it works for me:

    public class TestAppService : MyProjectNameAppService, IDistributedEventHandler<TestEventData>
    {
        private readonly IIdentityRoleRepository _identityRoleRepository;
        private readonly IDistributedEventBus _distributedEventBus;
    
        public TestAppService(IIdentityRoleRepository identityRoleRepository, IDistributedEventBus distributedEventBus)
        {
            _identityRoleRepository = identityRoleRepository;
            _distributedEventBus = distributedEventBus;
        }
    
        public virtual async Task PublishAsync()
        {
            await _distributedEventBus.PublishAsync(new TestEventData());
        }
    
        [UnitOfWork]
        public virtual async Task HandleEventAsync(TestEventData eventData)
        {
            try
            {
                await _identityRoleRepository.InsertAsync(new IdentityRole(Guid.NewGuid(), "test"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            
        }
    }
    

    I don't know about your project, Can you share a minimal project that reproduces the problem? thanks.

  • User Avatar
    0
    shijo created

    If I am using direct Repository it is working, but here i am going through ApplicationService class. I marked all methods with [UnitOfWork] still no luck.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Can you share a minimal project that reproduces the problem to make us progress? thanks.

  • User Avatar
    0
    shijo created

    Ok, I will make a small sample and share it with you. One more thing I noticed, I created a new Class inherited from ApplicationService and implemented IDistributedEventHandler, and paste all coding, which is working fine.

    The main difference between the two classes is One is inherited from DomainService and The Other is ApplicationService. If you have any clue just check.

    public class BookManager : DomainService Inside this class code is throwing exception

    public class BookAppDistEventService : ApplicationService, IDistributedEventHandler<APIBookEto> Inside this class code is working fine.

  • User Avatar
    0
    shijo created

    I managed to Implement IDistributedEventHandler inside BookManager class. Now it is working..

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06