4

我的设置是一个使用 .net 4.5 和 Entity Framework 5 的 asp.net mvc 4 项目。语言是 C#。

该代码主要是来自微软教程的直接副本,其中使用多格式数据来处理发布请求。发生的事情是,当我尝试访问文件的 ContentLength 时,它会引发 System.ObjectDisposedException。完整的堆栈跟踪遵循代码片段。

    public async Task<HttpResponseMessage> 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);
        }
    }

堆栈跟踪:

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.<PostFormData>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.<PostFormData>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.<PostFormData>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).

有一些建议是用几行修改 web.config 来解决问题,但是插入这些内容后,我发现它们不起作用。

我主要不是 microsoft / c# 人,因此我们将非常感谢您在回复中做出如此多的解释。

4

1 回答 1

0

我有同样的问题,并解决如下:

var fileSize = new FileInfo(file.LocalFileName).Length;

这段代码被添加到循环中而不是

Debug.WriteLine("This wont work: " + file.Headers.ContentLength);
于 2019-05-31T10:35:42.513 回答