问题
I'm working on a solution in which I want to export all the devices from an IoTHub to a BLOB.
Microsoft has an API for this and explains how to do this here.
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.
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
This is the code that I've used.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/42651680/azure-iothub-exportdevicesasync-throws-internal-server-error