-
I am playing with Cratis Chronicle in an Asp.Net project, and I am a noob to both this and Event Sourcing in general.
Here is my code: public class OrderSubmittedReactor(IPublishEndpoint publishEndpoint, ILogger<OrderSubmittedReactor> logger) : IReactor
{
public async Task Submitted(OrderSubmitted @event, EventContext context)
{
logger.LogInformation("Order {OrderId} has been submitted {@OrderSubmitted}", @event.OrderId, @event);
var orderCreated = new OrderCreated(Id.FromExisting(@event.OrderId), Id.NewId(), @event.Product, @event.Quantity, @event.OrderedAtUtc);
await publishEndpoint.Publish(orderCreated);
logger.LogInformation("Order created for {OrderId} has been published {@OrderCreated}", @event.OrderId, orderCreated);
}
} Update: Just wanted to add that ILogger resolves fine when I remove the IPublishEndpoint |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hey, the problem is that the Observers wrongfully uses the root service provider and not a scoped one when resolving the services. Your IPublishEndpoint is, probably correctly, registered as a scoped service which by design cannot be resolved from the root container. What you can do as a work around is to instead inject the IServiceProvider and in the Submitted method you can create and dispose your own service provider scope and use that to resolve your dependency. However as I said this is a fault in Chronicle and should be addressed |
Beta Was this translation helpful? Give feedback.
-
Thanks. Workaround works nicely for now :) |
Beta Was this translation helpful? Give feedback.
Hey, the problem is that the Observers wrongfully uses the root service provider and not a scoped one when resolving the services. Your IPublishEndpoint is, probably correctly, registered as a scoped service which by design cannot be resolved from the root container.
What you can do as a work around is to instead inject the IServiceProvider and in the Submitted method you can create and dispose your own service provider scope and use that to resolve your dependency.
However as I said this is a fault in Chronicle and should be addressed