c# - Azure IoT 中心导出设备异步引发内部服务器错误

标签 c# azure azure-iot-hub

我正在开发一个解决方案,希望将所有设备从 IoTHub 导出到 BLOB。

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

我已经在 10 台设备上执行了此代码,它工作正常,Azure 需要几秒钟来处理此代码,但除此之外它工作得很好。

但是,我在 S1 平台上使用超过 10 台设备(目前正在测试 100 台设备),该平台应该支持数量未定的设备

这是我使用过的代码。

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

是我的代码有问题,还是有其他问题在酝酿?

最佳答案

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

关于c# - Azure IoT 中心导出设备异步引发内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651680/

相关文章:

node.js - 在 Azure 应用服务上配置 Node 的默认文档

c - 链接非常简单的 MQTT 客户端(Azure IOT Hub SDK)时出现问题

c# - 缩略图 shell 扩展 .NET

c# - 在两个大括号结束之前将文本写入 .CS 文件

azure - Azure 时序见解 Gen2 的存储成本较高

azure - 通过 azure devops 中的管道将 terraform 部署到 azure

azure - 如何将具有不同时间戳属性的多个 Azure IoT 设备的数据引入同一个 Azure 时序见解环境?

azure - 将 azure 连接到外部 mqtt 代理

c# - 如何优化通过另一个列表绑定(bind)到 DTO 的 linq 查询?

c# - 滚动时自定义 TableViewCell 的高度被切断