我创建了一个Microsoft .Net Core 2.1控制台应用程序。这是我的代码
using System;
using Microsoft.Azure.EventHubs;
using System.Threading.Tasks;
using System.Threading;
using System.Text;
using System.Collections.Generic;
namespace bog_cs_AzureIoTMessageReader
{
class Program
{
private readonly static string s_eventHubsCompatibleEndpoint = "sb://foobar.servicebus.windows.net/";
private readonly static string s_eventHubsCompatiblePath = "...";
private readonly static string s_iotHubSasKey = "secret";
private readonly static string s_iotHubSasKeyName = "secret";
private static EventHubClient s_eventHubClient;
private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
{
// Create the receiver using the default consumer group.
// For the purposes of this sample, read only messages sent since
// the time the receiver is created. Typically, you don't want to skip any messages.
var eventHubReceiver = s_eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));
Console.WriteLine("Create receiver on partition: " + partition);
while (true)
{
if (ct.IsCancellationRequested) break;
Console.WriteLine("Listening for messages on: " + partition);
// Check for EventData - this methods times out if there is nothing to retrieve.
var events = await eventHubReceiver.ReceiveAsync(100);
// If there is data in the batch, process it.
if (events == null) continue;
foreach (EventData eventData in events)
{
string data = Encoding.UTF8.GetString(eventData.Body.Array);
Console.WriteLine("Message received on partition {0}:", partition);
Console.WriteLine(" {0}:", data);
Console.WriteLine("Application properties (set by device):");
foreach (var prop in eventData.Properties)
{
Console.WriteLine(" {0}: {1}", prop.Key, prop.Value);
}
Console.WriteLine("System properties (set by IoT Hub):");
foreach (var prop in eventData.SystemProperties)
{
Console.WriteLine(" {0}: {1}", prop.Key, prop.Value);
}
}
}
}
static async Task Main(string[] args)
{
Console.WriteLine("IoT Hub Quickstarts - Read device to cloud messages. Ctrl-C to exit.\n");
// Create an EventHubClient instance to connect to the
// IoT Hub Event Hubs-compatible endpoint.
var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);
s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());
// Create a PartitionReciever for each partition on the hub.
var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();
var d2cPartitions = runtimeInfo.PartitionIds;
CancellationTokenSource cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
Console.WriteLine("Exiting...");
};
var tasks = new List<Task>();
foreach (string partition in d2cPartitions)
{
tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
}
// Wait for all the PartitionReceivers to finsih.
Task.WaitAll(tasks.ToArray());
}
}
}
执行此操作时,我收到错误消息
1> ------开始构建:项目:bog-cs-AzureIoTMessageReader, 配置:调试任何CPU ------ 1> Program.cs(55,22,55,26):错误 CS8107:功能'异步主要'在C#7.0中不可用。请用 语言版本7.1或更高版本。 1> CSC:错误CS5001:程序执行 不包含适用于入口点的静态“ Main”方法1> Done 建筑项目“ bog-cs-AzureIoTMessageReader.csproj”-失败。 ===========构建:0成功,1失败,0最新,跳过0 ==========
这是怎么了?我该如何解决?
答案 0 :(得分:-1)
您必须将项目的LangVersion设置为至少7.1
<Project Sdk="Microsoft.NET.Sdk.Web">
...
<PropertyGroup>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
...
</Project>