Last active
March 19, 2019 17:29
-
-
Save micdenny/c1a93c7829a98ffb1d1bbddb980c50e1 to your computer and use it in GitHub Desktop.
EasyNetQ BusExtensions ConnectionAwaiter
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
public static class BusExtensions | |
{ | |
public static Task ConnectionAwaiter(this IBus bus) | |
{ | |
var cts = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); | |
bus.Advanced.Connected += (s, e) => | |
{ | |
cts.TrySetResult(true); | |
}; | |
if (bus.IsConnected) | |
{ | |
cts.TrySetResult(true); | |
} | |
return cts.Task; | |
} | |
} |
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var app = new Application(); | |
app.Run().Wait(); | |
} | |
} | |
public class Application | |
{ | |
public async Task Run() | |
{ | |
LogProvider.SetCurrentLogProvider(ConsoleLogProvider.Instance); | |
var bus = RabbitHutch.CreateBus("host=localhost;virtualHost=ConsoleApp6"); | |
await bus.ConnectionAwaiter(); | |
bus.SubscribeAsync<MyMessage>("test", msg => Task.CompletedTask); | |
Console.ReadLine(); | |
bus.Dispose(); | |
} | |
} | |
public class MyMessage { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can use this instead of
SpinWait.SpinUntil(() => bus.IsConnected);