Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用FileStream.CopyToAsync时无法访问关闭的文件?_C#_Async Await_C# 5.0 - Fatal编程技术网

C# 使用FileStream.CopyToAsync时无法访问关闭的文件?

C# 使用FileStream.CopyToAsync时无法访问关闭的文件?,c#,async-await,c#-5.0,C#,Async Await,C# 5.0,我有下面的方法, public Task SaveFileContentsAsync(string filePath, Stream stream) { using (var fileStream = new FileStream(filePath, FileMode.Create)) { return stream.CopyToAsync(fileStream); } } 此方法只需将流保存到磁盘

我有下面的方法,

    public Task SaveFileContentsAsync(string filePath, Stream stream)
    {
        using (var fileStream = new FileStream(filePath, FileMode.Create))
        {
            return stream.CopyToAsync(fileStream);
        }
    }
此方法只需将流保存到磁盘中。当我使用
HttpPostedFileBase.InputStream
调用此方法时

Exception Details: System.ObjectDisposedException: Cannot access a closed file.


[ObjectDisposedException: Cannot access a closed file.]
   System.IO.__Error.FileNotOpen() +57
   System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count) +10549618
   System.IO.Stream.<BeginWriteInternal>b__11(Object param0) +59
   System.Threading.Tasks.Task`1.InnerInvoke() +3132781
   System.Threading.Tasks.Task.Execute() +45
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.IO.Stream.EndWrite(IAsyncResult asyncResult) +85
   System.IO.FileStream.EndWrite(IAsyncResult asyncResult) +24
   System.IO.Stream.<BeginEndWriteAsync>b__17(Stream stream, IAsyncResult asyncResult) +24
   System.Threading.Tasks.FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization) +44
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.IO.<CopyToAsyncInternal>d__2.MoveNext() +253
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   S.Web.Controllers.<Add>d__15.MoveNext() in c:\Users\A\Source\Test\STest\S.Web\Controllers\ProductController.cs:66
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +50
   System.Web.Mvc.Async.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +53
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +53
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +53
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
异常详细信息:System.ObjectDisposedException:无法访问关闭的文件。
[ObjectDisposedException:无法访问已关闭的文件。]
System.IO.\u错误.FileNotOpen()+57
System.IO.FileStream.Write(字节[]数组,Int32偏移量,Int32计数)+10549618
System.IO.Stream.b__11(对象参数0)+59
System.Threading.Tasks.Task`1.InnerInvoke()+3132781
System.Threading.Tasks.Task.Execute()+45
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.IO.Stream.EndWrite(IAsyncResult asyncResult)+85
System.IO.FileStream.EndWrite(IAsyncResult asyncResult)+24
System.IO.Stream.b_uu17(Stream-Stream,IAsyncResult-asyncResult)+24
System.Threading.Tasks.FromAsyncTrimPromission`1.Complete(t支持thisRef、Func`3 endMethod、IAsyncResult asyncResult、Boolean requiresSynchronization)+44
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.IO.d_u2.MoveNext()+253
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.Runtime.CompilerServices.TaskWaiter.GetResult()+21
c:\Users\A\Source\Test\STest\S.Web\Controllers\ProductController.cs:66中的S.Web.Controllers.d\u 15.MoveNext()
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)+84
System.Web.Mvc.Async.c__显示类37.b__36(IAsyncResult asyncResult)+17
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)+10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32
System.Web.Mvc.Async.AsyncInvocationWithFilters.b_u3f()+50
System.Web.Mvc.Async.c_uudisplayClass48.b_uu41()+225
System.Web.Mvc.Async.c__显示Class33.b__32(IAsyncResult asyncResult)+10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)+10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34
System.Web.Mvc.Async.c__显示类2b.b__1c()+26
System.Web.Mvc.Async.c__显示Class21.b__1e(IAsyncResult asyncResult)+100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)+10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+27
System.Web.Mvc.Controller.b_u1d(IAsyncResult asyncResult,ExecuteCorerate innerState)+13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+53
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+39
System.Web.Mvc.Controller.b_u15(IAsyncResult asyncResult,Controller-Controller)+12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+28
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+53
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10
System.Web.Mvc.MvcHandler.b_uu5(IAsyncResult asyncResult,ProcessRequestState innerState)+21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+53
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+31
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9514812
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+155

这是因为流被放置在下一行。调用是异步的。。因此,在使用块的
结束时,会转到
Dispose
调用

wait
CopyToAsync
返回的
任务
,以便
fileStream
实例在执行其需要执行的操作时仍然可用:

public async Task SaveFileContentsAsync(string filePath, Stream stream)
{
    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        await stream.CopyToAsync(fileStream);
    }
}