Open Closed

Creating/Modifying entities in Event Handlers #8995


User avatar
0
icoretchi created
  • Template: microservice
  • Created ABP Studio Version: 0.9.24
  • Current ABP Studio Version: 0.9.25
  • Multi-Tenancy: Yes
  • UI Framework: angular
  • Theme: leptonx
  • Theme Style: system
  • Run Install Libs: Yes
  • Database Provider: ef
  • Database Management System: sqlserver
  • Mobile Framework: none
  • Public Website: No
  • Include Tests: Yes
  • Dynamic Localization: Yes
  • Kubernetes Configuration: Yes
  • Grafana Dashboard: Yes
  • Use Local References: No
  • Optional Modules:
    • GDPR
    • FileManagement
    • TextTemplateManagement
    • AuditLogging
    • Chat
    • OpenIddictAdmin
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Hi,

I am publishing event in one microservice and listen in another. I've noticed that when I create or delete entities in the distributed event handlers the CreatorId or DeletorId record fields not added. Any ideas?

Thanks.


7 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hi,

    Can you send your entity contents and if you have a configuration for AbpDistributedEntityEventOptions?

  • User Avatar
    0
    icoretchi created

    Hi,

    I don't have configuration for AbpDistributedEntityEventOptions

  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hi,

    CreatorId and DeletorId are auto-filled using ICurrentUser during HTTP requests. However, in distributed event handling, events are processed asynchronously in another microservice without the original HTTP context, so ICurrentUser data isn’t available, leaving these fields null. I suggest adding CreatorId and DeletorId to the ETO object when publishing the event and then setting them in the event handler as follows:

           test.CreatorId = eventData.Entity.CreatorId;
           test.DeleterId = eventData.Entity.DeleterId;
    
  • User Avatar
    0
    icoretchi created

    Hi,

    Even if I'll set them manually, as you suggest, the repository Update and Delete methods will set them to null.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi,

    Even if I'll set them manually, as you suggest, the repository Update and Delete methods will set them to null.

    Yes, you are right. Instead, you can try to change the current principal, which will change the CurrentUser as you stated in the related scope.

    We have a section in our documentation for changing the current principal, which you can check out at https://abp.io/docs/latest/framework/infrastructure/current-user#changing-the-current-principal

    You basically, need to inject the ICurrentPrincipalAccessor service, and use its Change method (which is a disposable method, so you should create a scope as stated in the documentation) and specify the CurrentUser as you wish. With this way, it should set the CreatorId and DeleterId.

    Let me know, if it fixes your problem. Regards.

  • User Avatar
    0
    icoretchi created

    Everything is working now. Thanks.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Everything is working now. Thanks.

    Great to hear that. Regards.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on April 16, 2025, 12:13