使用 .NET C# 7 和 VS 2017 检查通过 EventHubConsumerClient 发送的 S2C 消息

Check sent S2C messages with EventHubConsumerClient using .NET C# 7 and VS 2017

我从 IoT 中心和 Azure 服务开始。我正在学习这个快速入门教程:Quickstart: Send telemetry from a device to an IoT hub and read it with a back-end application (.NET)

created a Free Tier IoT Hub in my Azure account and registered a device using Azure Cloud Shell. Now I'm developing a "starter" library send D2C messages and I can see that it is possible to read sent messages。我在最后一步遇到问题,代码似乎是用 C# 8 编写的,但我正在使用 VS 2017 进行开发并在 await foreach 循环中出现错误。

示例代码是here,这是我要更改为C# 7 的代码:

private static async Task ReceiveMessagesFromDeviceAsync(CancellationToken cancellationToken)
{
    string connectionString = "{MyConnectionString}";
    await using EventHubConsumerClient consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionString, EventHubName);
    Console.WriteLine("Listening for messages on all partitions");

    try
    {
        await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync(cancellationToken))
        {
            ...
        }
    }
    catch (TaskCanceledException)
    {
        ...
    }
}

这是我在 Visual Studio 中得到的错误:

foreach 语句不能对类型 'IAsyncEnumerable' 的变量进行操作,因为 'IAsyncEnumerable' 不包含 'GetEnumerator'[=34= 的 public 实例定义]

是否可以重写此行以用于 C# 7?

一些细节:

  • Visual Studio 专业 2017
  • 库目标框架:.NET Core 2.1.NET 4.7.2

可以通过手动管理枚举来在没有 C# 8 的情况下使用库。基本形式类似于:

public static async Task Main(string[] args)
{
    var connection = "<< CONNECTION STRING >>" ;
    var hub = "<< EVENT HUB NAME >>";
    var producer = new EventHubProducerClient(connection, hub);
    var consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connection, hub);

    try
    {
        Console.WriteLine("Sending...");

        using (var batch = await producer.CreateBatchAsync())
        {
            for (var index = 0; index < 50; ++index)
            {
                batch.TryAdd(new EventData(Encoding.UTF8.GetBytes(index.ToString())));
            }

            await producer.SendAsync(batch);
        }

        Console.WriteLine("Reading...");

        var iterator = consumer.ReadEventsAsync(new ReadEventOptions { MaximumWaitTime = TimeSpan.FromMilliseconds(500) }).GetAsyncEnumerator();

        try
        {
            var consecutiveEmpties = 0;

            while (await iterator.MoveNextAsync())
            {
                var item = iterator.Current;

                if (item.Data != null)
                {
                    Console.WriteLine($"\tEvent: { Encoding.UTF8.GetString(item.Data.Body.ToArray()) }");
                    consecutiveEmpties = 0;
                }
                else if (++consecutiveEmpties > 5)
                {
                    break;
                }
            }
        }
        finally
        {
           await iterator.DisposeAsync();
        }

        Console.WriteLine("Done reading.");
    }
    finally
    {
        await producer.CloseAsync();
        await consumer.CloseAsync();
    }

    Console.ReadKey();
}

如果您愿意,也可以通过使用最新的编译器包将 C# 8 与 Visual Studio 2017 一起使用,如 问题的答案中所述。

抱歉;我们一直打算用一个样本来演示,但还没有机会实现它。更多上下文可以在 this GitHub 问题中找到。