Guest User

Untitled

a guest
May 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using NServiceBus;
  5.  
  6. class SerializedHandler : IHandleMessages<MyMessage>
  7. {
  8. static readonly SemaphoreSlim sequentialAccess = new SemaphoreSlim(1);
  9. static readonly TimeSpan waitDuration = TimeSpan.FromSeconds(5);
  10.  
  11. public async Task Handle(OrderReceived message, IMessageHandlerContext context)
  12. {
  13. try
  14. {
  15. var success = await sequentialAccess.WaitAsync(waitDuration)
  16. .ConfigureAwait(false);
  17.  
  18. if (!success) throw new InvalidOperationException($"Could not acquire access within {waitDuration}");
  19.  
  20. // Do my stuff here
  21. }
  22. finally
  23. {
  24. sequentialAccess.Release();
  25. }
  26. }
  27. }
Add Comment
Please, Sign In to add comment