C# 向事件中心UWP发送消息
我的问题 我需要将遥测数据发送到我使用UWP在Azure帐户中创建的EventHub 我创建了一个web应用程序(我在其中提供了有关EventHub连接和存储区域键的详细信息)——它从EventHub获取数据,并使用WebSocket绘制实时图形 我尝试过的 我有一个控制台应用程序,它使用ServiceBus dll将数据发送到EventHub。 当我尝试创建UWP时,核心.Net Framework不支持ServiceBus dllC# 向事件中心UWP发送消息,c#,azure,uwp,azure-eventhub,C#,Azure,Uwp,Azure Eventhub,我的问题 我需要将遥测数据发送到我使用UWP在Azure帐户中创建的EventHub 我创建了一个web应用程序(我在其中提供了有关EventHub连接和存储区域键的详细信息)——它从EventHub获取数据,并使用WebSocket绘制实时图形 我尝试过的 我有一个控制台应用程序,它使用ServiceBus dll将数据发送到EventHub。 当我尝试创建UWP时,核心.Net Framework不支持ServiceBus dll 您能给我看一些将数据发送到EventHub的指针或代码片段吗
您能给我看一些将数据发送到EventHub的指针或代码片段吗。在通用应用程序中,您必须使用新的Microsoft.Azure.EventHubs NuGet软件包 引述本文:
我也一直在引用这篇文章,我也看到消息到达EventHub。但是我需要这个设备(RaspberryPi3)来发送消息。如果我也使用此代码从Raspberry发送消息,我会看到消息(比如RASP_MSG)到达我的EventHub。但当我尝试订阅此数据RASP_MSG时,我无法在控制台应用程序或Web应用程序中接收它。注意:但是从我的代码(比如模拟发送者)发送的其他消息(RASP_MSG除外)可以通过相同的控制台App\Web App接收。我在这里遗漏了什么?任何想法请分享。这似乎是你的控制台应用程序端的一个问题。它正在侦听所有分区吗?对不起,我弄错了。在EventHub中,当数据使用上述代码(在UWP中)从设备发送到EventHub时,只会看到传入的请求,而不会看到任何消息,但会有一些小的更改。已完成的更改:==========>1。正在删除
GetAwaiter().GetResult()代码>来自主方法。==========>2.从wait eventHubClient.sendaync(新的EventData(Encoding.UTF8.GetBytes(message)))中删除wait代码>如果未进行上述更改(1)和(2),则系统将挂起,即它不会从等待eventHubClient.SendAsync(新的EventData…;
)移动。请指导我执行下一步操作。
namespace SampleSender
{
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
public class Program
{
private static EventHubClient eventHubClient;
private const string EhConnectionString = "{Event Hubs connection string}";
private const string EhEntityPath = "{Event Hub path/name}";
public static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
private static async Task MainAsync(string[] args)
{
// Creates an EventHubsConnectionStringBuilder object from a the connection string, and sets the EntityPath.
// Typically the connection string should have the Entity Path in it, but for the sake of this simple scenario
// we are using the connection string from the namespace.
var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
{
EntityPath = EhEntityPath
};
eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
await SendMessagesToEventHub(100);
await eventHubClient.CloseAsync();
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
// Creates an Event Hub client and sends 100 messages to the event hub.
private static async Task SendMessagesToEventHub(int numMessagesToSend)
{
for (var i = 0; i < numMessagesToSend; i++)
{
try
{
var message = $"Message {i}";
Console.WriteLine($"Sending message: {message}");
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
}
await Task.Delay(10);
}
Console.WriteLine($"{numMessagesToSend} messages sent.");
}
}
}
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));