How to defer an event in NServiceBus 6.0?

540 Views Asked by At

We're using NserviceBus as our messaging infrastructure with RabbitMQ as the transport. I'm trying to upgrade to NServiceBus 6.0 from 5.* version. In 5.0, we could defer events using "Bus.Defer()". But it seems like in 6.0 we can defer only messages but not events ??

If I use below code with message being an "event", I get an error saying that events should be published.

        var sendOptions = new SendOptions();
        sendOptions.DoNotDeliverBefore(DateTimeOffset.Now.AddMinutes(30));
        sendOptions.RouteToThisEndpoint();
        return context.Send(message, sendOptions);

but context.Publish(message, new PublishOptions()) method takes in "PublishOptions" which does not have an option to defer.

Am I missing something here ? Appreciate if someone could help.

2

There are 2 best solutions below

0
On BEST ANSWER

I've got an answer in another forum and I think it is the most relevant, So posting it here so that it could help someone in future. Thanks to Daniel Marbach

https://groups.google.com/forum/#!topic/particularsoftware/ivy1wdsycT8

Bus.Defer in v5 was internally always doing a send operation. It seems the difference to v6 is that it automatically disabled the messaging best practices. You can achieve the same by calling

        var sendOptions = new SendOptions();
        sendOptions.DoNotDeliverBefore(DateTimeOffset.Now.AddMinutes(30));
        sendOptions.RouteToThisEndpoint();
        sendOptions.DoNotEnforceBestPractices();
        return context.Send(message, sendOptions);

https://docs.particular.net/nservicebus/messaging/best-practice-enforcement

1
On

Some changes are not immediately effective, so we will have to defer some of those events.

The publisher should not be constrained by any of the subscribers.

Is it correct to assume that Product Authoring system publishes ProductDataUpdate events regardless of when the actual effective date will take place? In such case, you are already notified about a decision that was made. What are you, as a subscriber, going to do with it is a different thing and entirely internal.

You could send a command, for the sake of this discussion call it UpdateProductCost, that would be a delayed message if EffectiveDate is in the future. Otherwise, it's an immediate command.