Created
May 17, 2018 13:30
-
-
Save ramonsmits/491055d070d68d1bfb2e30537bcffce5 to your computer and use it in GitHub Desktop.
NServiceBus serialized handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using NServiceBus; | |
| class SerializedHandler : IHandleMessages<MyMessage> | |
| { | |
| static readonly SemaphoreSlim sequentialAccess = new SemaphoreSlim(1); | |
| static readonly TimeSpan waitDuration = TimeSpan.FromSeconds(5); | |
| public async Task Handle(OrderReceived message, IMessageHandlerContext context) | |
| { | |
| try | |
| { | |
| var success = await sequentialAccess.WaitAsync(waitDuration) | |
| .ConfigureAwait(false); | |
| if (!success) throw new InvalidOperationException($"Could not acquire access within {waitDuration}"); | |
| // Do my stuff here | |
| } | |
| finally | |
| { | |
| sequentialAccess.Release(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment