/// <summary> /// Deserialize the request model from the message body /// </summary> /// <param name="requestType">The type of the request model that we will try to activate</param> /// <returns>An instance of requestType</returns> public async Task <object> DeserializeRequestAsync(Type requestType) { // First create a context so that our formatter knows how to deserialize the model var context = new InputFormatterContext( _httpContext, string.Empty, new ModelStateDictionary(), requestType.MetaData() ); // Finally, have the formatter return a model from the http request body var inputFormatterResult = await _inputFormatter.ReadAsync(context); return(inputFormatterResult.Model ?? Activator.CreateInstance(requestType)); }
/// <summary> /// Asynchronously read the body content as the specified type. /// </summary> /// <typeparam name="TBody"> /// The CLR data-type that the body content will be deserialised into. /// </typeparam> /// <param name="content"> /// The <see cref="HttpContent"/> to read. /// </param> /// <param name="formatter"> /// The content formatter used to deserialise the body content. /// </param> /// <param name="formatterContext"> /// Contextual information about the content body being deserialised. /// </param> /// <returns> /// The deserialised body content. /// </returns> public static async Task <TBody> ReadAsAsync <TBody>(this HttpContent content, IInputFormatter formatter, InputFormatterContext formatterContext) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (formatterContext == null) { throw new ArgumentNullException(nameof(formatterContext)); } using (Stream responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false)) { object responseBody = await formatter.ReadAsync(formatterContext, responseStream).ConfigureAwait(false); return((TBody)responseBody); } }