private async Task OnReceiveRequest(Guid id, ReceiveRequest request) { // request is done, we can handle it if (_requestHandler != null) { var response = await _requestHandler.ProcessRequestAsync(request, null, context : _handlerContext).ConfigureAwait(false); if (response != null) { await _sendOperations.SendResponseAsync(id, response).ConfigureAwait(false); } } }
/// <summary> /// Reads the body of this <see cref="ReceiveRequest"/> as a string. /// </summary> /// <param name="request">The current instance of <see cref="ReceiveRequest"/>.</param> /// <returns>On success, a string populated with data read from the <see cref="ReceiveRequest"/> body. /// Otherwise null. /// </returns> public static string ReadBodyAsString(this ReceiveRequest request) { try { var contentStream = request.Streams.FirstOrDefault(); using (var reader = new StreamReader(contentStream.Stream, Encoding.UTF8)) { return(reader.ReadToEnd()); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Serializes the body of this <see cref="ReceiveRequest"/> as JSON. /// </summary> /// <typeparam name="T">The type to attempt to deserialize the contents of this <see cref="ReceiveRequest"/>'s body into.</typeparam> /// <param name="request">The current instance of <see cref="ReceiveRequest"/>.</param> /// <returns>On success, an object of type T populated with data serialized from the <see cref="ReceiveRequest"/> body. /// Otherwise a default instance of type T. /// </returns> public static T ReadBodyAsJson <T>(this ReceiveRequest request) { // The first stream attached to a ReceiveRequest is always the ReceiveRequest body. // Any additional streams must be defined within the body or they will not be // attached properly when processing activities. try { var contentStream = request.Streams.FirstOrDefault(); using (var reader = new StreamReader(contentStream.Stream, Encoding.UTF8)) { using (var jsonReader = new JsonTextReader(reader)) { var serializer = JsonSerializer.Create(SerializationSettings.DefaultDeserializationSettings); return(serializer.Deserialize <T>(jsonReader)); } } } catch (Exception ex) { throw ex; } }
#pragma warning disable IDE0034 /// <summary> /// The method that must be implemented in order to handle incoming requests. /// </summary> /// <param name="request">A <see cref="ReceiveRequest"/> for this handler to process.</param> /// <param name="logger">Logger.</param> /// <param name="context">Optional context to process the request within.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>A <see cref="Task"/> that will produce a <see cref="StreamingResponse"/> on successful completion.</returns> public abstract Task <StreamingResponse> ProcessRequestAsync(ReceiveRequest request, ILogger <RequestHandler> logger, object context = null, CancellationToken cancellationToken = default(CancellationToken));