Skip to content

Instantly share code, notes, and snippets.

@micdenny
Last active March 19, 2019 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micdenny/c1a93c7829a98ffb1d1bbddb980c50e1 to your computer and use it in GitHub Desktop.
Save micdenny/c1a93c7829a98ffb1d1bbddb980c50e1 to your computer and use it in GitHub Desktop.
EasyNetQ BusExtensions ConnectionAwaiter
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;
}
}
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 { }
@micdenny
Copy link
Author

you can use this instead of SpinWait.SpinUntil(() => bus.IsConnected);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment