Commenting out 2 GetModelFromMultipartRequest() and ReadAsMultipart() as for the moment they are not used and we need to rethink how to handle them

This commit is contained in:
Elitsa Marinovska
2021-01-13 16:16:11 +01:00
parent 3a51965358
commit 7c5e045868

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
@@ -23,29 +23,29 @@ namespace Umbraco.Web.WebApi
/// <param name="requestKey"></param> /// <param name="requestKey"></param>
/// <param name="validationKeyPrefix"></param> /// <param name="validationKeyPrefix"></param>
/// <returns></returns> /// <returns></returns>
public static T GetModelFromMultipartRequest<T>(this HttpActionContext actionContext, MultipartFormDataStreamProvider result, string requestKey, string validationKeyPrefix = "") //public static T GetModelFromMultipartRequest<T>(this HttpActionContext actionContext, MultipartFormDataStreamProvider result, string requestKey, string validationKeyPrefix = "")
{ //{
if (result.FormData[requestKey/*"contentItem"*/] == null) // if (result.FormData[requestKey/*"contentItem"*/] == null)
{ // {
var response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest); // var response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest);
response.ReasonPhrase = $"The request was not formatted correctly and is missing the '{requestKey}' parameter"; // response.ReasonPhrase = $"The request was not formatted correctly and is missing the '{requestKey}' parameter";
throw new HttpResponseException(response); // throw new HttpResponseException(response);
} // }
//get the string json from the request // //get the string json from the request
var contentItem = result.FormData[requestKey]; // var contentItem = result.FormData[requestKey];
//deserialize into our model // //deserialize into our model
var model = JsonConvert.DeserializeObject<T>(contentItem); // var model = JsonConvert.DeserializeObject<T>(contentItem);
//get the default body validator and validate the object // //get the default body validator and validate the object
var bodyValidator = actionContext.ControllerContext.Configuration.Services.GetBodyModelValidator(); // var bodyValidator = actionContext.ControllerContext.Configuration.Services.GetBodyModelValidator();
var metadataProvider = actionContext.ControllerContext.Configuration.Services.GetModelMetadataProvider(); // var metadataProvider = actionContext.ControllerContext.Configuration.Services.GetModelMetadataProvider();
//by default all validation errors will not contain a prefix (empty string) unless specified // //by default all validation errors will not contain a prefix (empty string) unless specified
bodyValidator.Validate(model, typeof(T), metadataProvider, actionContext, validationKeyPrefix); // bodyValidator.Validate(model, typeof(T), metadataProvider, actionContext, validationKeyPrefix);
return model; // return model;
} //}
/// <summary> /// <summary>
/// Helper method to get the <see cref="MultipartFormDataStreamProvider"/> from the request in a non-async manner /// Helper method to get the <see cref="MultipartFormDataStreamProvider"/> from the request in a non-async manner
@@ -53,54 +53,54 @@ namespace Umbraco.Web.WebApi
/// <param name="actionContext"></param> /// <param name="actionContext"></param>
/// <param name="rootVirtualPath"></param> /// <param name="rootVirtualPath"></param>
/// <returns></returns> /// <returns></returns>
public static MultipartFormDataStreamProvider ReadAsMultipart(this HttpActionContext actionContext, string rootVirtualPath) //public static MultipartFormDataStreamProvider ReadAsMultipart(this HttpActionContext actionContext, string rootVirtualPath)
{ //{
if (actionContext.Request.Content.IsMimeMultipartContent() == false) // if (actionContext.Request.Content.IsMimeMultipartContent() == false)
{ // {
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); // throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
} // }
var hostingEnvironment = Current.Factory.GetRequiredService<IHostingEnvironment>(); // var hostingEnvironment = Current.Factory.GetRequiredService<IHostingEnvironment>();
var root = hostingEnvironment.MapPathContentRoot(rootVirtualPath); // var root = hostingEnvironment.MapPathContentRoot(rootVirtualPath);
//ensure it exists // //ensure it exists
Directory.CreateDirectory(root); // Directory.CreateDirectory(root);
var provider = new MultipartFormDataStreamProvider(root); // var provider = new MultipartFormDataStreamProvider(root);
var request = actionContext.Request; // var request = actionContext.Request;
var content = request.Content; // var content = request.Content;
// Note: YES this is super strange, ugly, and weird. // // Note: YES this is super strange, ugly, and weird.
// One would think that you could just do: // // One would think that you could just do:
// // //
//var result = content.ReadAsMultipartAsync(provider).Result; // //var result = content.ReadAsMultipartAsync(provider).Result;
// // //
// But it deadlocks. See https://stackoverflow.com/questions/15201255 for details, which // // But it deadlocks. See https://stackoverflow.com/questions/15201255 for details, which
// points to https://msdn.microsoft.com/en-us/magazine/jj991977.aspx which contains more // // points to https://msdn.microsoft.com/en-us/magazine/jj991977.aspx which contains more
// details under "Async All the Way" - see also https://olitee.com/2015/01/c-async-await-common-deadlock-scenario/ // // details under "Async All the Way" - see also https://olitee.com/2015/01/c-async-await-common-deadlock-scenario/
// which contains a simplified explanation: ReadAsMultipartAsync is meant to be awaited, // // which contains a simplified explanation: ReadAsMultipartAsync is meant to be awaited,
// not used in the non-async .Result way, and there is nothing we can do about it. // // not used in the non-async .Result way, and there is nothing we can do about it.
// // //
// Alas, model binders cannot be async "all the way", so we have to wrap in a task, to // // Alas, model binders cannot be async "all the way", so we have to wrap in a task, to
// force proper threading, and then it works. // // force proper threading, and then it works.
MultipartFormDataStreamProvider result = null; // MultipartFormDataStreamProvider result = null;
var task = Task.Run(() => content.ReadAsMultipartAsync(provider)) // var task = Task.Run(() => content.ReadAsMultipartAsync(provider))
.ContinueWith(x => // .ContinueWith(x =>
{ // {
if (x.IsFaulted && x.Exception != null) // if (x.IsFaulted && x.Exception != null)
{ // {
throw x.Exception; // throw x.Exception;
} // }
result = x.ConfigureAwait(false).GetAwaiter().GetResult(); // result = x.ConfigureAwait(false).GetAwaiter().GetResult();
}, // },
// Must explicitly specify this, see https://blog.stephencleary.com/2013/10/continuewith-is-dangerous-too.html // // Must explicitly specify this, see https://blog.stephencleary.com/2013/10/continuewith-is-dangerous-too.html
TaskScheduler.Default); // TaskScheduler.Default);
task.Wait(); // task.Wait();
if (result == null) // if (result == null)
throw new InvalidOperationException("Could not read multi-part message"); // throw new InvalidOperationException("Could not read multi-part message");
return result; // return result;
} //}
} }
} }