The setup I have is an asp.net mvc 4 project using .net 4.5 and Entity Framework 5. The language is C#.
The code is mainly a straight copy from the microsoft tutorials with using multiform data to handle the post requests. What is happening is that when I attempt to access the ContentLength of the file, it throws a System.ObjectDisposedException. The full stacktrace follows the code snippet.
public async Task PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Debug.WriteLine(file.Headers.ContentDisposition.FileName);
Debug.WriteLine("Server file path: " + file.LocalFileName);
Debug.WriteLine("Content type: " + file.Headers.ContentType);
Debug.WriteLine("This wont work: " + file.Headers.ContentLength);
}
return Request.CreateResponse(HttpStatusCode.OK, "OK");
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
Stacktrace:
A first chance exception of type 'System.ObjectDisposedException' occurred in System.Net.Http.dll
iisexpress.exe Error: 0 : Operation=ReflectedHttpActionDescriptor.ExecuteAsync, Exception=System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Http.StringContent'.
at System.Net.Http.HttpContent.CheckDisposed()
at System.Net.Http.HttpContent.GetComputedOrBufferLength()
at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()
at FieldAssistant.Controllers.SpeedTestController.d__0.MoveNext() in c:\Users\matt.delves\blah\FieldAssistant\Controllers\SpeedTestController.cs:line 48
iisexpress.exe Error: 0 : Operation=ApiControllerActionInvoker.InvokeActionAsync, Exception=System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Http.StringContent'.
at System.Net.Http.HttpContent.CheckDisposed()
at System.Net.Http.HttpContent.GetComputedOrBufferLength()
at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()
at FieldAssistant.Controllers.SpeedTestController.d__0.MoveNext() in c:\Users\matt.delves\blah\FieldAssistant\Controllers\SpeedTestController.cs:line 48
iisexpress.exe Error: 0 : Operation=SpeedTestController.ExecuteAsync, Exception=System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Http.StringContent'.
at System.Net.Http.HttpContent.CheckDisposed()
at System.Net.Http.HttpContent.GetComputedOrBufferLength()
at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()
at FieldAssistant.Controllers.SpeedTestController.d__0.MoveNext() in c:\Users\matt.delves\blah\FieldAssistant\Controllers\SpeedTestController.cs:line 48
iisexpress.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
iisexpress.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
iisexpress.exe Information: 0 : Response, Status=500 (InternalServerError), Method=POST, Url=http://192.168.2.63:51230/api/speedtest, Message='Content-type='application/json; charset=utf-8', content-length=unknown'
iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync
iisexpress.exe Information: 0 : Operation=SpeedTestController.Dispose
The program '[4436] iisexpress.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
There have been some suggestions that modifying the web.config with a couple of lines are meant to resolve the issue but having inserted those, I found that they didn't work.
I'm not primarily a microsoft / c# guy so as much explanation in the response would be greatly appreciated.