Пример #1
0
        private static async Task <HttpStatusCode> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation, string hubName, string listenConnectionString, CancellationToken cancellationToken)
        {
            if (deviceInstallation.installationId == null)
            {
                return(HttpStatusCode.BadRequest);
            }

            ConnectionStringUtility connectionSaSUtil = new ConnectionStringUtility(listenConnectionString);
            string hubResource = "installations/" + deviceInstallation.installationId + "?";
            string apiVersion  = "api-version=2016-07";

            string uri = connectionSaSUtil.Endpoint + hubName + "/" + hubResource + apiVersion;

            string SasToken = connectionSaSUtil.getSaSToken(uri, 60);

            using (var httpClient = new HttpClient())
            {
                string json = JsonConvert.SerializeObject(deviceInstallation);

                httpClient.DefaultRequestHeaders.Add("Authorization", SasToken);

                var response = await httpClient.PutAsync(uri, new StringContent(json, System.Text.Encoding.UTF8, "application/json"));

                return(response.StatusCode);

                // return HttpStatusCode.OK;
            }
        }