2017-08-25 8 views
0

でのIoTハブからデータを取得すると、この簡単な方法は、私はMSのチュートリアル<a href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-getstarted" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-getstarted</a>からのコードを使用してみてくださいC#の

internal class Program 
{ 
private static string connectionString = "HostName=...="; 
private static string d2cEndpoint = "messages/events"; 
private static EventHubClient eventHubClient; 

private static void Main(string[] args) 
{ 
    Console.WriteLine("Receive messages\n"); 
    eventHubClient = EventHubClient. 
     CreateFromConnectionString(
      connectionString, d2cEndpoint); 

    var d2cPartitions = eventHubClient. 
      GetRuntimeInformation().PartitionIds; 

    foreach (string partition in d2cPartitions) 
    { 
     ReceiveMessagesFromDeviceAsync(partition); 
    } 
    Console.ReadLine(); 
} 

private async static Task ReceiveMessagesFromDeviceAsync(
             string partition) 
{ 
    var eventHubReceiver = eventHubClient. 
      GetDefaultConsumerGroup(). 
      CreateReceiver(partition, DateTime.UtcNow); 
    while (true) 
    { 
     EventData eventData = await eventHubReceiver. 
              ReceiveAsync(); 
     if (eventData == null) continue; 

     string data = Encoding.UTF8.GetString(
            eventData.GetBytes()); 
     Console.WriteLine(string.Format(
      "Message received. Partition: {0} Data: '{1}'", 
      partition, data)); 
    } 
} 
} 

はIoTをハブからデータを取得するには、まだ何かが間違っていました。 このデータを簡単に取得することは可能ですか?

+2

「まだ何かが間違っていました」 - >まさに何か。あなたは例外を受け取りますか? "何か"を定義する –

+0

私たちは本当にいくつかのエラーコードを出力する必要があります。 – Jay

+0

@PeterBonsプログラムの開始後の戻りエラー "未処理の例外:System.IO.FileNotFoundExpection:ファイルまたはアセンブリ 'Microsoft.Service.Bus、Version 3.0.0.0、Culture = neutral、PublicKeyToken = XXX'またはその依存関係の1つを読み込めませんでした。 " dllはexeでディレクトリにあります。なにが問題ですか? – ksk

答えて

1

VS2017バージョン> = 15.3.1を使用して、のEventHubTriggerのAzure関数プロジェクトを作成することができます。

using Microsoft.Azure.WebJobs; 
using Microsoft.Azure.WebJobs.Host; 
using Microsoft.Azure.WebJobs.ServiceBus; 

namespace FunctionApp4 
{ 
public static class Function1 
{ 
    [FunctionName("Function1")] 
    public static void Run([EventHubTrigger("myEventHubName", Connection = "myIoTHub")]string myEventHubMessage, TraceWriter log) 
    { 
     log.Info($"C# Event Hub trigger function processed a message: {myEventHubMessage}"); 
    } 
} 

}

とlocal.settings.jsonファイル:

{ 
    "IsEncrypted": false, 
    "Values": { 
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...", 
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...", 
    "myIoTHub": "Endpoint=sb://....servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=..." 
    } 
} 

は、次の画面スニペットはローカルアズール機能のコンソール出力を示し、次のテンプレートコードスニペットは、この機能を示しています: Function1

関連する問題

 関連する問題