Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- }
Add Comment
Please, Sign In to add comment