private async Task <IActionResult> SendDeviceRequestAsync(string deviceId) { IActionResult result = null; await m_TelemetryService.IncrementCounterAsync("senddevicerequest", new string[] { }); var request = await m_TelemetryService.StartTimerAsync(() => m_DeviceService.CreateRequestAsync(deviceId, this.Request), "create_request", new string[] { }); var response = await m_TelemetryService.StartTimerAsync(() => m_DeviceService.InvokeRequestAsync(deviceId, request), "invoke_request", new string[] { }); switch (response) { case DeviceInlineResponse inlineResponse: result = new ContentResult() { Content = inlineResponse.Body, StatusCode = response.Headers.Status, ContentType = response.Headers.ContentType }; break; case DeviceFileResponse fileResponse: result = File(await m_TelemetryService.StartTimerAsync(() => m_FileService.OpenFileAsync(deviceId, fileResponse.BlobUrl), "blob_storage.read_file", new string[] { }), response.Headers.ContentType); break; default: throw new NotSupportedException($"Unsupported response type: {response}"); } // set headers that are not handled by the IActionResult implementation this.Response.SetHeadersFromDeviceResponse(response.Headers); m_TelemetryService.Dispose(); return(result); }
private void Dispose(bool isDisposing) { if (!isDisposing) { return; } _telemetryService.Dispose(); _flightService.Dispose(); _client.Dispose(); _disposer?.Dispose(); }
/// <summary> /// Creates a device request based on the incoming HTTP request. /// </summary> /// <param name="deviceId"></param> /// <param name="request"></param> /// <returns></returns> public async Task <DeviceRequest> CreateRequestAsync(string deviceId, HttpRequest request) { // discard any body that could have been provided when doing GET or DELETE requests if (HttpMethods.IsGet(request.Method) || HttpMethods.IsDelete(request.Method)) { return(new DeviceInlineRequest(request)); } // determine whether the request body needs to be written to file storage or sent inline to the device. // a more accurate check could be done against the serialized DeviceRequest instead of the current request.ContentLength since additional info is added to the payload later on. // that would still not be perfectly accurate since the SDK adds its own data to the final payload. if (request.HasFormContentType || request.ContentLength.GetValueOrDefault() > m_DeviceCommunicationAdapter.GetMaximumMessageSize()) { var fileData = new BlobStreamDecorator(request.Body) { ContentType = request.ContentType }; var blobSasUrl = string.Empty; await m_TelemetryService.IncrementCounterAsync("blob_upload", new string[] { }); blobSasUrl = await m_TelemetryService.StartTimerAsync(() => m_FileService.WriteFileAsync(deviceId, fileData), "blob_upload", new string[] { }); m_TelemetryService.Dispose(); return(new DeviceFileRequest(request) { BlobUrl = blobSasUrl }); } else { var inlineRequest = new DeviceInlineRequest(request); using (StreamReader reader = new StreamReader(request.Body, inlineRequest.Headers.GetEncoding())) { inlineRequest.Body = await reader.ReadToEndAsync(); } return(inlineRequest); } }