public async Task FileUpload_SmallFile_Http_GranularSteps_x509()
        {
            string filename = await GetTestFileNameAsync(FileSizeSmall).ConfigureAwait(false);

            using var fileStreamSource = new FileStream(filename, FileMode.Open, FileAccess.Read);
            var fileUploadTransportSettings = new Http1TransportSettings();

            await UploadFileGranularAsync(fileStreamSource, filename, fileUploadTransportSettings, x509auth : true).ConfigureAwait(false);
        }
        public async Task Message_DeviceSendSingleMessage_Http_WithCustomeProxy()
        {
            Http1TransportSettings httpTransportSettings = new Http1TransportSettings();
            CustomWebProxy proxy = new CustomWebProxy();
            httpTransportSettings.Proxy = proxy;
            ITransportSettings[] transportSettings = new ITransportSettings[] { httpTransportSettings };

            await SendSingleMessage(transportSettings).ConfigureAwait(false);
            Assert.AreNotEqual(proxy.Counter, 0);
        }
        public async Task Message_DeviceSendSingleMessage_Http_WithCustomProxy()
        {
            var httpTransportSettings = new Http1TransportSettings();
            var proxy = new CustomWebProxy(Logger);

            httpTransportSettings.Proxy = proxy;
            var transportSettings = new ITransportSettings[] { httpTransportSettings };

            await SendSingleMessage(TestDeviceType.Sasl, transportSettings).ConfigureAwait(false);

            Assert.AreNotEqual(proxy.Counter, 0);
        }
 internal HttpTransportHandler(IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
 {
     this.transportSettings = transportSettings;
     this.deviceId          = iotHubConnectionString.DeviceId;
     this.httpClientHelper  = new HttpClientHelper(
         iotHubConnectionString.HttpsEndpoint,
         iotHubConnectionString,
         ExceptionHandlingHelper.GetDefaultErrorMapping(),
         DefaultOperationTimeout,
         null,
         this.transportSettings.ClientCertificate);
 }
 internal HttpTransportHandler(IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
 {
     this.transportSettings = transportSettings;
     this.deviceId = iotHubConnectionString.DeviceId;
     this.httpClientHelper = new HttpClientHelper(
         iotHubConnectionString.HttpsEndpoint,
         iotHubConnectionString,
         ExceptionHandlingHelper.GetDefaultErrorMapping(),
         DefaultOperationTimeout,
         null,
         this.transportSettings.ClientCertificate);
 }
        public async Task FileUpload_SmallFile_Http_GranularSteps_Proxy()
        {
            string filename = await GetTestFileNameAsync(FileSizeSmall).ConfigureAwait(false);

            using var fileStreamSource = new FileStream(filename, FileMode.Open, FileAccess.Read);
            var fileUploadTransportSettings = new Http1TransportSettings()
            {
                Proxy = new WebProxy(Configuration.IoTHub.ProxyServerAddress)
            };

            await UploadFileGranularAsync(fileStreamSource, filename, fileUploadTransportSettings).ConfigureAwait(false);
        }
예제 #7
0
        private async Task UploadFileGranularAsync(Stream source, string filename, Http1TransportSettings fileUploadTransportSettings, bool useX509auth = false)
        {
            using TestDevice testDevice = await TestDevice.GetTestDeviceAsync(
                      Logger,
                      _devicePrefix,
                      useX509auth?TestDeviceType.X509 : TestDeviceType.Sasl).ConfigureAwait(false);

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

            X509Certificate2 cert = null;
            DeviceAuthenticationWithX509Certificate x509Auth = null;

            if (useX509auth)
            {
                cert     = s_selfSignedCertificate;
                x509Auth = new DeviceAuthenticationWithX509Certificate(testDevice.Id, cert);

                deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, x509Auth, Client.TransportType.Http1);
            }
            else
            {
                deviceClient = DeviceClient.CreateFromConnectionString(testDevice.ConnectionString, Client.TransportType.Http1, clientOptions);
            }

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

            using (deviceClient)
            {
                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);
            }

            x509Auth?.Dispose();
        }
        public async Task FileUpload_SmallFile_Http_WithProxy()
        {
            string smallFile = await GetTestFileNameAsync(FileSizeSmall).ConfigureAwait(false);

            var httpTransportSettings = new Http1TransportSettings()
            {
                Proxy = new WebProxy(Configuration.IoTHub.ProxyServerAddress)
            };

            ITransportSettings[] transportSettingsWithProxy = { httpTransportSettings };

            await UploadFile(transportSettingsWithProxy, smallFile).ConfigureAwait(false);
        }
예제 #9
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);
        }
예제 #10
0
        public DeviceClient Create()
        {
            // Set up a proxy for DPS if specified
            if (!String.IsNullOrEmpty(_proxyOptions?.ProxyUri))
            {
                // Configure IoT Hub/Central Proxy
                Http1TransportSettings transportSettings = new Http1TransportSettings();
                transportSettings.Proxy = new WebProxy(_proxyOptions.ProxyUri);
                ITransportSettings[] transportSettingsArray = new ITransportSettings[] { transportSettings };

                return(DeviceClient.Create(
                           _registrationProvider.AssignedHub,
                           _authenticationMethod,
                           transportSettingsArray
                           ));
            }

            return(DeviceClient.Create(
                       _registrationProvider.AssignedHub,
                       _authenticationMethod
                       ));
        }
예제 #11
0
        // Connect to IoT Hub/Central and send a test message
        private static async Task ConnectAndSend(DeviceProvisioningProxySettings settings)
        {
            // Configure DPS Transport Proxy
            var transport = new ProvisioningTransportHandlerHttp();

            transport.Proxy = new WebProxy(settings.ProxyUri);

            // Create DPS Client
            var security           = new SecurityProviderSymmetricKey(settings.RegistrationId, settings.PrimaryKey, settings.SecondaryKey);
            var provisioningClient = ProvisioningDeviceClient.Create(settings.GlobalDeviceEndpoint, settings.IdScope, security, transport);

            // Register Device
            var registrationResult = await provisioningClient.RegisterAsync().ConfigureAwait(false);

            // Configure Device Authentication from result
            var auth = new DeviceAuthenticationWithRegistrySymmetricKey(registrationResult.DeviceId, security.GetPrimaryKey());

            // Configure IoT Hub/Central Proxy
            Http1TransportSettings transportSettings = new Http1TransportSettings();

            transportSettings.Proxy = new WebProxy(settings.ProxyUri);
            ITransportSettings[] transportSettingsArray = new ITransportSettings[] { transportSettings };

            // Connect to IoT Hub/Central and send a message
            var messageText = "{ \"test\": \"hello\" }";
            var message     = new Message(Encoding.UTF8.GetBytes(messageText));

            using (var deviceClient = DeviceClient.Create(registrationResult.AssignedHub, auth, transportSettingsArray)) {
                try {
                    await deviceClient.SendEventAsync(message);

                    Console.WriteLine("Message sent");
                } finally {
                    Console.WriteLine("Done");
                }
            }
        }
        internal HttpTransportHandler(
            PipelineContext context,
            IotHubConnectionString iotHubConnectionString,
            Http1TransportSettings transportSettings,
            HttpClientHandler httpClientHandler  = null,
            bool isClientPrimaryTransportHandler = false)
            : base(context, transportSettings)
        {
            ProductInfo productInfo = context.ProductInfo;

            _deviceId         = iotHubConnectionString.DeviceId;
            _moduleId         = iotHubConnectionString.ModuleId;
            _httpClientHelper = new HttpClientHelper(
                iotHubConnectionString.HttpsEndpoint,
                iotHubConnectionString,
                ExceptionHandlingHelper.GetDefaultErrorMapping(),
                s_defaultOperationTimeout,
                null,
                transportSettings.ClientCertificate,
                httpClientHandler,
                productInfo,
                transportSettings.Proxy,
                isClientPrimaryTransportHandler);
        }
예제 #13
0
 internal HttpTransportHandler(IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
     : this(iotHubConnectionString)
 {
     this.transportSettings = transportSettings;
 }
예제 #14
0
        public void TransportSettingsTest_TransportType_Http()
        {
            var transportSetting = new Http1TransportSettings();

            Assert.IsTrue(transportSetting.GetTransportType() == TransportType.Http1, "Should be TransportType.Http1");
        }
예제 #15
0
        internal HttpTransportHandler(IPipelineContext context, IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings, HttpClientHandler httpClientHandler = null)
            : base(context, transportSettings)
        {
            ProductInfo productInfo = context.Get <ProductInfo>();

            this.deviceId         = iotHubConnectionString.DeviceId;
            this.moduleId         = iotHubConnectionString.ModuleId;
            this.httpClientHelper = new HttpClientHelper(
                iotHubConnectionString.HttpsEndpoint,
                iotHubConnectionString,
                ExceptionHandlingHelper.GetDefaultErrorMapping(),
                DefaultOperationTimeout,
                null,
                transportSettings.ClientCertificate,
                httpClientHandler,
                productInfo,
                transportSettings.Proxy);
        }
예제 #16
0
 internal HttpTransportHandler(IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
     :this(iotHubConnectionString)
 {
     this.transportSettings = transportSettings;
 }
예제 #17
0
 internal HttpTransportHandler(IPipelineContext context, IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
     : base(context, transportSettings)
 {
     this.TransportSettings = transportSettings;
     this.deviceId          = iotHubConnectionString.DeviceId;
     this.httpClientHelper  = new HttpClientHelper(
         iotHubConnectionString.HttpsEndpoint,
         iotHubConnectionString,
         ExceptionHandlingHelper.GetDefaultErrorMapping(),
         DefaultOperationTimeout,
         null);
 }