2017-02-08 5 views
1

私の問題へのメッセージを送信イベントハブUWP

私はUWPを使用して、私のAzureのアカウントで作成したEventHubにテレメトリデータを送信する必要があります。

私は、EventHubからデータを取得し、WebSocketを使用してリアルタイムグラフをプロットする、EventHub接続& Storage Areaキーに関する詳細を与えたWebアプリケーションを作成しました。私はEventHubにデータを送信するためにServiceBus DLLを使用するコンソールアプリケーションを持っている

を試してみましたが、何

。 UWPを作成しようとしたとき、ServiceBus dllはCore .Net Frameworkでサポートされていません

EventHubにデータを送信するポインタまたはコードスニペットを表示できますか?

答えて

1

ユニバーサルアプリケーションでは、新しいMicrosoft.Azure.EventHubs NuGetパッケージを使用する必要があります。

この記事から引用: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."); 
     } 
    } 
} 

ですから、NuGetパッケージをインストールし、接続文字列からEventHubClientを作成し、メッセージを送信するためにそれを使用する:

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

私が言及されていますこの記事でも、EventHubに届くメッセージが表示されています。しかし、メッセージを送信するにはデバイス(RaspberryPi3)が必要です。 このコードを使用してラズベリーからメッセージを送信しても、メッセージ(RASP_MSG)がEventHubに届いています。 しかし、このデータRASP_MSGを購読しようとすると、コンソールアプリケーションまたはWebアプリケーションで受信できません。 注:私のコードから送信された他のメッセージ(RASP_MSG以外)(Simulated_SENDERなど)は、同じConsole App \ Web Appを介して受信できます。 ここに欠けているギャップは何ですか?どのようなアイデアを共有してください。 – jAntoni

+0

これは、あなたのコンソールアプリケーションの問題のようです。すべてのパーティションを聴いていますか? – juunas

+0

申し訳ありませんが私は間違っていました。 EventHubでは、_Incoming Requests_のみが表示され、上記のコードを使用してデバイスから(UWP内の)EventHubにデータが送信されたときに_messages_は表示されません。 変更が完了しました: ========= 1。メインメソッドから 'GetAreiter()。GetResult();'を削除します。 から========> 2.removingの_await_ 'eventHubClient.SendAsyncを待つ(新しいEventDataの(Encoding.UTF8.GetBytes(メッセージ)));' 場合、上記の変更(1)および(2 )が作成されていない場合、システムはハングします。つまり、 'await eventHubClient.SendAsync(new EventData ...;)から移動しません。 次の手順を案内してください。 – jAntoni

関連する問題