Search code examples
nservicebusnservicebus6nservicebus-rabbitmq

How to defer an event in NServiceBus 6.0?


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.


Solution

  • 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