[axonframework] Running into Cannot retrieve current AggregateLifecycle; none is yet defined

Hi Bharat,

It means you’re trying to apply an event whilst you don’t have a UnitOfWork.
More code specific, you’re calling either AggregateLifecycle.apply(), AggregateLifecycle.isLive() or 'AggregateLifecycle.markDeleted()` whilst you don’t have a UnitOfWork started, which probably means you’re calling one of those functions outside of an aggregate.

However, a code example would work better to tell you what’s going wrong.
Could you share a snippit of code of the ‘command and event handlers defined’?

Cheers,

Steven

My bad. I figured out the issue. I was calling it outside of an aggregate. Thanks for your response.

Hi Bharat,

No problem! Here to help everybody with Axon so :slight_smile:
If you’ve got any future questions don’t hesitate to ask them.

Cheers,
Steven

Hi Steven.

I got the same error.

It’s a Spring application, my class is annotated as @Aggregate, method is annotated as @CommandHandler.

Are you invoking your aggregate directly, or only by sending it a Command?

Not sure what the correct answer is.

I’m doing something like:

new Profile(new CreateProfile(id));

where Profile is an aggregate, and CreateProfile - command. Is it invoking my aggregate directly?

Hello,
Try injecting a CommandGateway into the class making this call. Then, replace that line with: commandGateway.send(new CreateProfile(id));

Hi Stephan,

What Allard means to ask is if you’re wiring the Aggregate Repository yourself to retrieve an aggregate to make it handle commands or if you let Axon do all the work.
From your short snippet however I think you’re trying to create the aggregate yourself, so ‘invoking directly’ would have been the case.

You’re however not inclined to call the constructor of an aggregate directly at all, the Repository implementation will take care of that.
You can just publish the CreateProfile command on the CommandBus (or through the CommandGateway) and Axon will know to call the command handling constructor of the Profile aggregate.
Internally it will then create the UnitOfWork, which will start the Aggregates Lifecycle, thus solving the exception you’re getting.

If this doesn’t solve your issue, please be more specific of your Axon application configuration and the version you’re using.

Nonetheless, I hope this helps!

Cheers,
Steven

Hi Stephan, Brian,

I see Brian was just ahead of me :wink:
What he’s saying is correct though, try that, it will most likely solve the issue.

Cheers,
Steven

Hi guys.

Both options works perfectly - CommandBus, and CommandGateway.

Thank you!