简体   繁体   中英

Send Message to Event Hub UWP

My Problem

I need to send the Telemetry data to EventHub that I have created in my Azure Account using UWP.

I have created an web app (where I have given details on EventHub connection & Storage Area keys) - that which gets the data from EventHub and plots a real time graph using WebSocket.

What I have tried

I have a console app which uses ServiceBus dll to send data to EventHub. When I tried to make a UWP the ServiceBus dll is not supported on Core .Net Framework

Can you show me some pointers or code snippet that would send data to EventHub.

In Universal Apps, you have to use the new Microsoft.Azure.EventHubs NuGet package.

Quoting from this article: https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send

 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.");
         }
     }
 }

So you install the NuGet package, create an EventHubClient from the connection string, and then use it to send messages:

await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM