Ejemplo n.º 1
0
        private async Task UploadFileAsync(Client.TransportType transport, string filename, bool x509auth = false)
        {
            TestDevice testDevice = await TestDevice.GetTestDeviceAsync(
                _devicePrefix,
                x509auth?TestDeviceType.X509 : TestDeviceType.Sasl).ConfigureAwait(false);

            DeviceClient deviceClient;

            if (x509auth)
            {
                X509Certificate2 cert = Configuration.IoTHub.GetCertificateWithPrivateKey();

                var auth = new DeviceAuthenticationWithX509Certificate(testDevice.Id, cert);
                deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, auth, transport);
            }
            else
            {
                deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString, transport);
            }

            await FileNotificationTestListener.InitAsync().ConfigureAwait(false);

            using (deviceClient)
            {
                using (var fileStreamSource = new FileStream(filename, FileMode.Open, FileAccess.Read))
                {
                    await deviceClient.UploadToBlobAsync(filename, fileStreamSource).ConfigureAwait(false);
                }

                await FileNotificationTestListener.VerifyFileNotification(filename, testDevice.Id).ConfigureAwait(false);

                await deviceClient.CloseAsync().ConfigureAwait(false);
            }
        }
        private async Task UploadFileDisconnectTransport(
            Client.TransportType transport,
            string filename,
            string faultType,
            string reason,
            int delayInSec,
            int durationInSec           = 0,
            int retryDurationInMilliSec = FaultInjection.RecoveryTimeMilliseconds)
        {
            TestDevice testDevice = await TestDevice.GetTestDeviceAsync(DevicePrefix).ConfigureAwait(false);

            using var deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString, transport);

            deviceClient.OperationTimeoutInMilliseconds = (uint)retryDurationInMilliSec;

            await FileNotificationTestListener.InitAsync().ConfigureAwait(false);

            using (var fileStreamSource = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                Task fileUploadTask     = deviceClient.UploadToBlobAsync(filename, fileStreamSource);
                Task errorInjectionTask = SendErrorInjectionMessageAsync(deviceClient, faultType, reason, delayInSec, durationInSec);
                await Task.WhenAll(fileUploadTask, errorInjectionTask).ConfigureAwait(false);

                await FileNotificationTestListener.VerifyFileNotification(filename, testDevice.Id).ConfigureAwait(false);
            }

            try
            {
                await deviceClient.CloseAsync().ConfigureAwait(false);
            }
            catch
            {
                // catch and ignore exceptions resulted incase device client close failed while offline
            }
        }
Ejemplo n.º 3
0
        private async Task UploadFileGranularAsync(Stream source, string filename, Http1TransportSettings fileUploadTransportSettings, bool x509auth = false)
        {
            await FileNotificationTestListener.InitAsync().ConfigureAwait(false);

            TestDevice testDevice = await TestDevice.GetTestDeviceAsync(
                _devicePrefix,
                x509auth?TestDeviceType.X509 : TestDeviceType.Sasl).ConfigureAwait(false);

            DeviceClient deviceClient;
            var          clientOptions = new ClientOptions()
            {
                FileUploadTransportSettings = fileUploadTransportSettings
            };

            if (x509auth)
            {
                X509Certificate2 cert = Configuration.IoTHub.GetCertificateWithPrivateKey();

                var auth = new DeviceAuthenticationWithX509Certificate(testDevice.Id, cert);

                // The X509 certificate being used for device authentication needs to be set into FileUploadTransportSettings as well,
                // so that the HttpClient created for file upload operation has access to those certificates.
                clientOptions.FileUploadTransportSettings.ClientCertificate = cert;
                deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, auth, Client.TransportType.Http1, clientOptions);
            }
            else
            {
                deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString, Client.TransportType.Http1, clientOptions);
            }

            var fileUploadSasUriRequest = new FileUploadSasUriRequest()
            {
                BlobName = filename
            };

            FileUploadSasUriResponse fileUploadSasUriResponse = await deviceClient.GetFileUploadSasUriAsync(fileUploadSasUriRequest).ConfigureAwait(false);

            var  blob       = new CloudBlockBlob(fileUploadSasUriResponse.GetBlobUri());
            Task uploadTask = blob.UploadFromStreamAsync(source);
            await uploadTask.ConfigureAwait(false);

            var notification = new FileUploadCompletionNotification
            {
                CorrelationId = fileUploadSasUriResponse.CorrelationId,
                IsSuccess     = uploadTask.IsCompleted
            };

            await deviceClient.CompleteFileUploadAsync(notification).ConfigureAwait(false);

            await FileNotificationTestListener.VerifyFileNotification(filename, testDevice.Id).ConfigureAwait(false);
        }