我有一个c#应用程序从azure获取数据,但是我需要一个dll才能执行此操作,我对异步任务和返回数据有很大的问题。
我的dll代码:
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
namespace ConsoleApp1
{
public class Program
{
private static string connectionString = "XXXX";
private static string d2cEndpoint = "messages/events";
private static EventHubClient eventHubClient;
public async static Task<string> read()
{
eventHubClient = EventHubClient.
CreateFromConnectionString(
connectionString, d2cEndpoint);
var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;
// wait for data from async task
string task = await ReceiveMessagesFromDeviceAsync(d2cPartitions[0]);
// return data from DLL
return task;
}
private async static Task<string> ReceiveMessagesFromDeviceAsync(string partition)
{
var eventHubReceiver = eventHubClient.
GetDefaultConsumerGroup().
CreateReceiver(partition, DateTime.UtcNow);
EventData eventData = await eventHubReceiver.ReceiveAsync();
string data = Encoding.UTF8.GetString(eventData.GetBytes());
return data;
}
}
}
问题是从这个dll获取数据 - 没有错误,但也没有数据(我认为无需等待从云端获取数据)
var result = Program.read();
MessageBox.Show(Convert.ToString(result), "Some title", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
有什么问题?有什么想法吗?