Asp.net mvc 4 System.ObjectDisposedException:必须为此操作打开存储

Asp.net mvc 4 System.ObjectDisposedException:必须为此操作打开存储,asp.net-mvc-4,openxml,isolatedstorage,Asp.net Mvc 4,Openxml,Isolatedstorage,IIS7 Windows2008机器上有一个MVC4应用程序。应用程序作为ApplicationPoolIdentity运行,相应的用户可以访问ProgramData\IsolatedStorage文件夹。 它还拥有C:\Documents and Settings\Default User\Local Settings\Application Data\IsolatedStorage文件夹的权限。 但在使用openxml编写大型excel文件时仍会收到异常 ERROR MHDB.MvcAppl

IIS7 Windows2008机器上有一个MVC4应用程序。应用程序作为ApplicationPoolIdentity运行,相应的用户可以访问ProgramData\IsolatedStorage文件夹。 它还拥有C:\Documents and Settings\Default User\Local Settings\Application Data\IsolatedStorage文件夹的权限。 但在使用openxml编写大型excel文件时仍会收到异常

ERROR MHDB.MvcApplication - System.ObjectDisposedException: Store must be open for this operation.
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
at MS.Internal.IO.Packaging.PackagingUtilities.SafeIsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, ReliableIsolatedStorageFileFolder folder)
at MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount, String& fileName)
at MS.Internal.IO.Packaging.SparseMemoryStream.EnsureIsolatedStoreStream()
at MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary()
at MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Packaging.CompressStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Xml.XmlUtf8RawTextWriter.FlushBuffer()
at System.Xml.XmlUtf8RawTextWriter.RawText(Char* pSrcBegin, Char* pSrcEnd)
at System.Xml.XmlUtf8RawTextWriter.WriteEndElement(String prefix, String localName, String ns)
at System.Xml.XmlWellFormedWriter.WriteEndElement()
at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo(XmlWriter w)
at DocumentFormat.OpenXml.OpenXmlElement.WriteTo(XmlWriter xmlWriter)
at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo(XmlWriter w)
at DocumentFormat.OpenXml.OpenXmlElement.WriteTo(XmlWriter xmlWriter)
at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo(XmlWriter w)
at DocumentFormat.OpenXml.OpenXmlPartRootElement.WriteTo(XmlWriter xmlWriter)
at DocumentFormat.OpenXml.OpenXmlPartRootElement.SaveToPart(OpenXmlPart openXmlPart)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents()
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(Boolean disposing)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()
at BL.OpenXML.Export(DataTable dt, Stream fs, Boolean createNewDocument)
at BL.BusinessLogic.CreateOpReport(UnitOfWork uow, String user, String orgapath, List`1 orgaids, Boolean isfin, String cycle, String startperiod, String endperiod)
at MHDB.Controllers.HomeController.GetFileAsStream()
at MHDB.Controllers.HomeController.ExportExcel()
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass13.<InvokeActionMethodWithFilters>b__10()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
错误MHDB.mvcapapplication-System.ObjectDisposedException:必须为此操作打开存储。
位于System.IO.IsolatedStorage.IsolatedStorage FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、IsolatedStorage文件isf)
在System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、IsolatedStorageFile isf)
位于MS.Internal.IO.Packaging.PackagingUtilities.SafeIsolatedStorageFileStream..ctor(字符串路径、文件模式、文件访问权限、文件共享、ReliableSolatedStorageFileFolder文件夹)
位于MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount、字符串和文件名)
在MS.Internal.IO.Packaging.SparseMemoryStream.ensureIsolatedStream()中
在MS.Internal.IO.Packaging.SparseMemoryStream.switchModeif-needed()处
在MS.Internal.IO.Packaging.compressionStream.Write处(字节[]缓冲区、Int32偏移量、Int32计数)
在MS.Internal.IO.Packaging.CompressStream.Write处(字节[]缓冲区,Int32偏移量,Int32计数)
在MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write处(字节[]缓冲区,Int32偏移量,Int32计数)
在MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write处(字节[]缓冲区、Int32偏移量、Int32计数)
位于System.Xml.XmlUtf8RawTextWriter.FlushBuffer()处
位于System.Xml.XmlUtf8RawTextWriter.RawText(Char*pSrcBegin,Char*pSrcEnd)
位于System.Xml.XmlUtf8RawTextWriter.WriteEndElement(字符串前缀、字符串localName、字符串ns)
位于System.Xml.XmlWellFormedWriter.WriteEndElement()处
位于DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContento(XmlWriter w)
位于DocumentFormat.OpenXml.openxmlement.WriteTo(XmlWriter)
位于DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContento(XmlWriter w)
位于DocumentFormat.OpenXml.openxmlement.WriteTo(XmlWriter)
位于DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContento(XmlWriter w)
位于DocumentFormat.OpenXml.OpenXmlPartRootElement.WriteTo(XmlWriter-XmlWriter)
位于DocumentFormat.OpenXml.OpenXmlPartRootElement.SaveToPart(OpenXmlPart-OpenXmlPart)
位于DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents()处
位于DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(布尔disposing)
位于DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()处
在BL.OpenXML.Export(数据表dt、流fs、布尔createNewDocument)
在BL.BusinessLogic.CreateOpReport(UnitOfWork uow、字符串用户、字符串orgapath、列表'1 orgaids、布尔值isfin、字符串周期、字符串起始周期、字符串结束周期)
在MHDB.Controllers.HomeController.GetFileAsStream()中
在MHDB.Controllers.HomeController.ExportExcel()中
在lambda_方法中(闭包、控制器基、对象[])
位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass13.b_uuu10()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1筛选器,ActionDescriptor ActionDescriptor,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)
在System.Web.Mvc.Controller.ExecuteCore()上
在System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)中
在System.Web.Mvc.Async.AsyncResultRapper.c__DisplayClass1.b__0()中
在System.Web.Mvc.MvcHandler.c_uuu显示class8.b_uuu3(IAsyncResult asyncResult)
在System.Web.Mvc.Async.AsyncResultRapper.c__DisplayClass4.b__3(IAsyncResult ar)中
在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()中
在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)

谢谢你的帮助

由于System.IO.Packaging,当您在内部写入大于10MB的文件时,它将不再临时将数据保存在内存中,直到下次保存,而是将切换到独立存储。这本身可能会导致很多问题,因为IsolateStorage不适用于同一应用程序的多线程/多个实例,并且您可能会面临一些权限问题

考虑到这是一个web应用程序,我认为它可能与多个用户同时写入有关,但都被解析到同一个孤立的存储位置

我偶然发现了这个问题,同一个应用程序的多个实例都被解析到了同一个位置。我仍在寻找一个优雅的解决方案,但我能够强制将每个实例解析到不同的IsolatedStorage位置,并通过使用以下命令使其唯一:

var rootDirUserField= typeof(IsolatedStorageFile).GetField("s_RootDirUser", BindingFlags.NonPublic | BindingFlags.Static);
rootDirUserField.SetValue(null, "<unique location in isolated storage>");
var rootDirUserField=typeof(IsolatedStorageFile).GetField(“s_RootDirUser”,BindingFlags.NonPublic | BindingFlags.Static);
rootDirUserField.SetValue(null,“”);
这是因为静态字段IsolatedStorageFile.s_RootDirUser用于解析要使用的新目录。我的修复特定于用户|域|程序集范围


我渴望找到更好的解决方案来解决此问题。

请查看我对此问题的答案。我希望我的问题和你的一样。你好谢谢你的链接。我想我会用新的包构建自己的开放式xml sdk