2017-03-07 56 views
1

我正在研究一种解决方案,我要将所有设备从IoTHub导出到BLOB。Azure IoTHub ExportDevicesAsync引发内部服务器错误

Microsoft为此提供了一个API并解释了如何执行此操作here

我已经为10个设备执行了此代码,并且它工作正常,Azure处理此操作需要几秒钟,否则它工作得很好。

但是我有超过10个设备(目前有100台设备测试)一S1平台上,这应该支持的设备

这个数量不确定的工作是我使用的代码。

的Program.cs

private static void Main(string[] args) 
    { 
     IoTExporter.ExportIoTDevices(); 
    } 

IoTExporter

public class IoTExporter 
{ 
    private const string Containername = "iot"; 

    public static void ExportIoTDevices() 
    { 
     // Create a blobclient which is used to connect to the blob storage. 
     var blobClient = CreateBlobClient(); 

     //Get a reference to a container to use for the sample code, and create it if it does not exist. 
     var container = blobClient.GetContainerReference(Containername); 
     container.CreateIfNotExists(); 

     //Generate a SAS token and assign it to the current job. 
     var storageUri = GetContainerSasUri(container); 
     CreateJob(storageUri); 

     Console.ReadLine(); 
    } 

    /// <summary> 
    /// Create a blobclient which is used to connect to the blob storage. 
    /// </summary> 
    /// <returns>A Blob client.</returns> 
    private static CloudBlobClient CreateBlobClient() 
    { 
     var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("BlobConnString")); 
     return storageAccount.CreateCloudBlobClient(); 
    } 

    private static string GetContainerSasUri(CloudBlobContainer container) 
    { 
     ConsoleWriter.WriteLine("Generating Uri"); 

     // Set constraints on the SAS token. 
     var sasConstraints = new SharedAccessBlobPolicy 
     { 
      SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10), 
      Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read | 
          SharedAccessBlobPermissions.Delete 
     }; 

     var sasContainerToken = container.GetSharedAccessSignature(sasConstraints); 

     //Return the URI string for the container, including the SAS token. 
     return container.Uri + sasContainerToken; 
    } 

    private static async void CreateJob(string storageUri) 
    { 
     ConsoleWriter.WriteLine("Creating Job"); 
     var manager = RegistryManager.CreateFromConnectionString(ConfigurationManager.AppSettings["IoT:ConnectionString"]); 

     await TestConnection(manager); 

     ConsoleWriter.WriteLine("Initiating Job"); 
     var job = await manager.ExportDevicesAsync(storageUri, "devices.txt", false); 
     await DoJob(job, manager); 
    } 

    private static async Task TestConnection(RegistryManager manager) 
    { 
     ConsoleWriter.WriteLine("Testing if connected to IoTHub"); 
     var devices = await manager.GetDevicesAsync(1); 
     if (!devices.Any()) 
     { 
      Environment.Exit(-1); 
     } 

     else 
     { 
      ConsoleWriter.WriteLine("IoT connected"); 
     } 
    } 

    private static async Task DoJob(JobProperties job, RegistryManager manager) 
    { 
     while (true) 
     { 
      job = await manager.GetJobAsync(job.JobId); 
      switch (job.Status) 
      { 
       case JobStatus.Completed: 
        FileWriter.WriteBlobToFile(GetContainer()); 
        ConsoleWriter.WriteLine($"Job {job.Status}"); 
        break; 
       case JobStatus.Failed: 
        ConsoleWriter.WriteLine($"Job failed due to {job.FailureReason}"); 
        break; 
       case JobStatus.Cancelled: 
        ConsoleWriter.WriteLine($"Job {job.Status}"); 
        break; 
       default: 
        ConsoleWriter.WriteLine($"Status of job: {job.Status}"); 
        await Task.Delay(TimeSpan.FromSeconds(5)); 
        continue; 
      } 
      break; 
     } 
    } 

    private static CloudBlobContainer GetContainer() 
    { 
     var blobClient = CreateBlobClient(); 

     // Retrieve a reference to a container and give it blob permissions. 
     var container = blobClient.GetContainerReference(Containername); 
     return container; 
    }}} 

ConsoleWriter

public static class ConsoleWriter 
{ 
    public static void WriteLine(string line) 
    { 
     var date = DateTime.Now; 
     var toWrite = $"{date} : {line}"; 
     Console.WriteLine(toWrite); 
    } 
} 

是我的代码PROBL时间,还是有其他东西在锅里酝酿?

+0

我使用F1-Free层上的100个设备测试您的代码,它可以工作。你详细的错误信息是什么? –

+0

事实证明,我工作的服务器(西欧)出现故障。我在北欧进行了测试,现在可以使用! – Azgramah

回答

2

事实证明,我工作的服务器(西欧)出现故障。我在北欧进行了测试,现在可以使用。

相关问题