Last active
July 8, 2019 07:27
-
-
Save davidfowl/0a8cbb17b7a2a5308a82ebcba1f364d7 to your computer and use it in GitHub Desktop.
Event loop flipped inside out with IAsyncEnumerable
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.ServiceBus; | |
namespace ConsoleApp60 | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var client = new QueueClent(connectionString, BasicQueueName, ReceiveMode.PeekLock); | |
await foreach (var message in client.GetQueueEnumerable()) | |
{ | |
try | |
{ | |
if (message.Label != null && | |
message.ContentType != null && | |
message.Label.Equals("Scientist", StringComparison.InvariantCultureIgnoreCase) && | |
message.ContentType.Equals("application/json", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
var body = message.Body; | |
JsonElement scientist = JsonSerializer.Deserialize<JsonElement>(body); | |
lock (Console.Out) | |
{ | |
Console.ForegroundColor = ConsoleColor.Cyan; | |
Console.WriteLine( | |
"\t\t\t\tMessage received: \n\t\t\t\t\t\tMessageId = {0}, \n\t\t\t\t\t\tSequenceNumber = {1}, \n\t\t\t\t\t\tEnqueuedTimeUtc = {2}," + | |
"\n\t\t\t\t\t\tExpiresAtUtc = {5}, \n\t\t\t\t\t\tContentType = \"{3}\", \n\t\t\t\t\t\tSize = {4}, \n\t\t\t\t\t\tContent: [ firstName = {6}, name = {7} ]", | |
message.MessageId, | |
message.SystemProperties.SequenceNumber, | |
message.SystemProperties.EnqueuedTimeUtc, | |
message.ContentType, | |
message.Size, | |
message.ExpiresAtUtc, | |
scientist.GetProperty("firstName").GetString(), | |
scientist.GetProperty("name").GetString()); | |
Console.ResetColor(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Exception: \"{0}\"", ex.Message); | |
} | |
} | |
} | |
} | |
} |
This file contains 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 void RegisterCallback(Func<Item, Task> callback) | |
{ | |
while (true) | |
{ | |
try | |
{ | |
var item = await DequeueAsync(); | |
await callback(item); | |
} | |
catch (Exception ex) | |
{ | |
DeadLetter(ex); | |
} | |
} | |
} |
This file contains 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 IAsyncEnumerable<Item> GetQueueAsyncEnumerable() | |
{ | |
while (true) | |
{ | |
var item = await DequeueAsync(); | |
yield return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment