public async Task <IActionResult> GetEntry(int id, [FromQuery] UserParams userParams) { try { var blogEntry = await _repo.GetBlogEntry(id); if (blogEntry.IsDeleted) { return(BadRequest("Entry Bulunamadı")); } var blogEntryDto = _mapper.Map <BlogEntryDto>(blogEntry); ConvertPhotoUrl(new List <BlogEntryDto>() { blogEntryDto }, 500); var a = new MethodCallResponse <BlogEntryDto>() { entity = blogEntryDto, Message = null, StatusCode = ReturnCodes.OK }; return(Ok(a)); } catch (System.Exception ex) { Extensions.ReportError(ex); } return(StatusCode(500)); }
/// <summary> /// Makes a method request from a message /// </summary> /// <param name="message"></param> /// <param name="timeout"></param> /// <param name="ct"></param> /// <returns></returns> internal async Task <Message> InvokeDeviceMethodAsync(Message message, TimeSpan timeout, CancellationToken ct) { #endif if (string.IsNullOrEmpty(message.DeviceId)) { throw ProxyEventSource.Log.ArgumentNull("deviceId"); } var buffer = new MemoryStream(); var request = new MethodCallRequest(message, timeout); request.Encode(buffer, CodecId.Json); var uri = new UriBuilder { Scheme = "https", Host = _hubConnectionString.HostName, Path = $"/twins/{WebUtility.UrlEncode(message.DeviceId)}/methods", Query = "api-version=" + _apiVersion }; MethodCallResponse response; try { var stream = await _http.StreamAsync(uri.Uri, Http.Post, async h => { h.Add(HttpRequestHeader.Authorization.ToString(), await GetSasTokenAsync(3600).ConfigureAwait(false)); h.Add(HttpRequestHeader.UserAgent.ToString(), _clientId); }, (s, h) => { }, ct, Encoding.UTF8.GetString(buffer.ToArray()), "application/json").ConfigureAwait(false); using (stream) { response = MethodCallResponse.Decode(stream, CodecId.Json); } } catch (HttpResponseException hex) { if (hex.StatusCode != HttpStatusCode.NotFound) { ProxyEventSource.Log.HandledExceptionAsError(this, hex); throw new ProxyException( $"Remote proxy device method communication failure: {hex.StatusCode}.", hex); } else { ProxyEventSource.Log.HandledExceptionAsInformation(this, hex); throw new ProxyNotFoundException(hex); } } catch (OperationCanceledException) { throw; } catch (Exception ex) { ProxyEventSource.Log.HandledExceptionAsError(this, ex); throw new ProxyException($"Unknown proxy failure.", ex); } if (response.Status != 200) { throw ProxyEventSource.Log.Rethrow( new ProxyException($"Unexpected status {response.Status} returned by proxy.")); } return(response.Payload); }
private void Server_OnMessageReceived(byte[] bytes) { var json = Encoding.ASCII.GetString(bytes); PipeMessage message = JsonConvert.DeserializeObject <PipeMessage>(json); //get method call data //call implementation and get response //send response (if error goes here, need to throw something to the client. Also need to time this out on the client side) var request = message.GetPayload <MethodCallRequest>(); object result = null; try { MethodInfo invokeMethod = typeof(T).GetMethod(request.MethodName); for (int i = 0; i < request.ParameterValues.Count; i++) { var jObject = request.ParameterValues[i] as JObject; if (jObject != null) { request.ParameterValues[i] = jObject.ToObject(request.ParameterTypes[i]); } else if (request.ParameterTypes[i].BaseType == typeof(Enum)) { request.ParameterValues[i] = Convert.ChangeType(request.ParameterValues[i], typeof(int)); } else { request.ParameterValues[i] = Convert.ChangeType(request.ParameterValues[i], request.ParameterTypes[i]); } } result = invokeMethod.Invoke(instance, request.ParameterValues.ToArray()); } catch (Exception ex) { request.ReturnType = typeof(ServerException); result = ex.InnerException?.Message; } var response = new MethodCallResponse() { MethodName = request.MethodName, ReturnType = request.ReturnType, ReturnValue = result }; var responseMessage = new PipeMessage <MethodCallResponse>(response); var responseJson = JsonConvert.SerializeObject(responseMessage); byte[] responseBytes = Encoding.ASCII.GetBytes(responseJson); server.Send(responseBytes); }
private void Server_OnMessageReceived(PipeMessage e) { //get method call data //call implementation and get response //send response (if error goes here, need to throw something to the client. Also need to time this out on the client side) var request = e.GetPayload <MethodCallRequest>(); object result = null; try { MethodInfo invokeMethod = typeof(T).GetMethod(request.MethodName); for (int i = 0; i < request.ParameterValues.Count; i++) { var jObject = request.ParameterValues[i] as JObject; if (jObject != null) { request.ParameterValues[i] = jObject.ToObject(request.ParameterTypes[i]); } else { request.ParameterValues[i] = Convert.ChangeType(request.ParameterValues[i], request.ParameterTypes[i]); } } result = invokeMethod.Invoke(instance, request.ParameterValues.ToArray()); } catch (Exception ex) { request.ReturnType = typeof(ServerException); result = ex.InnerException?.Message; } var response = new MethodCallResponse() { MethodName = request.MethodName, ReturnType = request.ReturnType, ReturnValue = result }; server.Send(new PipeMessage <MethodCallResponse>(response)); }
/// <summary> /// Возвращает строку с читабельным результатом выполнения запроса. /// </summary> /// <param name="response">Результат выполнения запроса.</param> /// <typeparam name="TArguments">Тип данных ответа.</typeparam> /// <returns>Cтроку читабельным результатом выполнения запроса.</returns> public static string GetOkOrResult <TArguments>(this MethodCallResponse <TArguments> response) { return(response.Result.ToLower() == OkResult ? OkMessage : $"Ответ сервера: {response.Result}"); }