Skip to content

Instantly share code, notes, and snippets.

@danielmarbach
Last active October 11, 2017 07:41
Show Gist options
  • Save danielmarbach/3031de0986e9b3e874cf74f607bce0c0 to your computer and use it in GitHub Desktop.
Save danielmarbach/3031de0986e9b3e874cf74f607bce0c0 to your computer and use it in GitHub Desktop.
Example sync timeouts
namespace Server
{
public class Handler : IHandleMessages<SynchroniseSite>
{
public IProvideSites SiteProvider { get; set; }
public async Task Handle(SynchroniseSite message, IMessageHandlerContext context)
{
try
{
List<Data> data = new List<Data>();
// assuming connection is sync and long-running
await Task.Run(() => {
var connection = SiteProvider.GetConnection(message.SiteId);
data.AddRange(connection.GetSomeData());
})
// awaiting the above will throw any exceptions happening in the task body
// so if we reached here we know the connection is online
await context.Publish("Connection is online");
// just showing the LINQ way for education purposes, but be aware this might lead to more allocations
var tasks = data.Select(item => context.Publish("Got some new info")).ToArray();
await Task.WhenAll(tasks);
}
catch (Exception exception)
{
//This will be a specific connection exception
await context.Publish("Connection is offline");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment