private async Task <HttpResponseMessage> HandlePostEntityOperationRequestAsync( HttpRequestMessage request, EntityId entityId) { IDurableEntityClient client = this.GetClient(request); string operationName; if (request.Method == HttpMethod.Delete) { operationName = "delete"; } else { operationName = request.GetQueryNameValuePairs()["op"] ?? string.Empty; } if (request.Content == null || request.Content.Headers.ContentLength == 0) { await client.SignalEntityAsync(entityId, operationName); return(request.CreateResponse(HttpStatusCode.Accepted)); } else { string requestContent = await request.Content.ReadAsStringAsync(); string mediaType = request.Content.Headers.ContentType?.MediaType; object operationInput; if (string.Equals(mediaType, "application/json", StringComparison.OrdinalIgnoreCase)) { try { operationInput = MessagePayloadDataConverter.ConvertToJToken(requestContent); } catch (JsonException e) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not parse JSON content: " + e.Message)); } } else { operationInput = requestContent; } await client.SignalEntityAsync(entityId, operationName, operationInput); return(request.CreateResponse(HttpStatusCode.Accepted)); } }
/// <summary> /// Gets the input of the current activity function instance as a <c>JToken</c>. /// </summary> /// <returns> /// The parsed <c>JToken</c> representation of the activity input. /// </returns> internal JToken GetInputAsJson() { if (this.serializedInput != null && this.parsedJsonInput == null) { var objectArray = this.messageDataConverter.Deserialize <object[]>(this.serializedInput); if (objectArray?.Length != 1) { throw new ArgumentException("The serialized input is expected to be an object array with one element."); } this.parsedJsonInput = MessagePayloadDataConverter.ConvertToJToken(this.messageDataConverter.Serialize(objectArray[0])); } return(this.parsedJsonInput); }
private async Task <HttpResponseMessage> HandleRaiseEventRequestAsync( HttpRequestMessage request, string instanceId, string eventName) { IDurableOrchestrationClient client = this.GetClient(request); DurableOrchestrationStatus status = await client.GetStatusAsync(instanceId); if (status == null) { return(request.CreateResponse(HttpStatusCode.NotFound)); } switch (status.RuntimeStatus) { case OrchestrationRuntimeStatus.Failed: case OrchestrationRuntimeStatus.Canceled: case OrchestrationRuntimeStatus.Terminated: case OrchestrationRuntimeStatus.Completed: return(request.CreateResponse(HttpStatusCode.Gone)); } string mediaType = request.Content.Headers.ContentType?.MediaType; if (!string.Equals(mediaType, "application/json", StringComparison.OrdinalIgnoreCase)) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Only application/json request content is supported")); } string stringData = await request.Content.ReadAsStringAsync(); object eventData; try { eventData = MessagePayloadDataConverter.ConvertToJToken(stringData); } catch (JsonReaderException e) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid JSON content", e)); } await client.RaiseEventAsync(instanceId, eventName, eventData); return(request.CreateResponse(HttpStatusCode.Accepted)); }
internal static JToken ParseToJToken(string value) { if (value == null) { return(NullJValue); } // Ignore whitespace value = value.Trim(); if (value.Length == 0) { return(string.Empty); } try { return(MessagePayloadDataConverter.ConvertToJToken(value)); } catch (JsonReaderException) { // Return the raw string value as the fallback. This is common in terminate scenarios. return(value); } }