本文介绍了Azure IoTHub ExportDevicesAsync引发内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种解决方案,其中我希望将所有设备从IoTHub导出到BLOB.

I'm working on a solution in which I want to export all the devices from an IoTHub to a BLOB.

Microsoft为此提供了一个API,并说明了如何执行此操作此处.

Microsoft has an API for this and explains how to do this here.

我已经为10台设备执行了此代码,并且可以正常工作,Azure需要花费几秒钟的时间来处理它,否则,它就可以正常工作.

I've executed this code for 10 devices and it works fine, it takes a few seconds for Azure to process this but otherwise it works just fine.

但是我在S1平台上使用了10多个设备(目前正在测试100个设备),这应该支持数量不确定的设备

However I'm working with more than 10 devices (currently testing with 100 devices) on a S1 platform, which should support an undetermined amount of devices

这是我使用的代码.

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

我的代码是问题所在,还是罐中还有其他东西正在酝酿?

Is my code the problem, or is there something else brewing in the pot?

推荐答案

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

It turns out that the server upon which I was working (West-Europe) had a malfunction. I tested it on North Europe and now it works.

这篇关于Azure IoTHub ExportDevicesAsync引发内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:13