C# 使用WordprocessingDocument错误:无法创建互斥
我正在使用此简单模式在ASP.NET应用程序中创建docx文件:C# 使用WordprocessingDocument错误:无法创建互斥,c#,.net,openxml,docx,openxml-sdk,C#,.net,Openxml,Docx,Openxml Sdk,我正在使用此简单模式在ASP.NET应用程序中创建docx文件: var outputFileName = "creating some file name here..."; var outputFile = string.Format("~/App_Data/files/{0}.docx", outputFileName); // creating a file stream to write to var outputStream = new FileStream(HttpContext
var outputFileName = "creating some file name here...";
var outputFile = string.Format("~/App_Data/files/{0}.docx", outputFileName);
// creating a file stream to write to
var outputStream = new FileStream(HttpContext.Current.Server.MapPath(outputFile), FileMode.OpenOrCreate);
// creating the default template
using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/some-template.docx"), Encoding.UTF8)) {
var l = (int)sr.BaseStream.Length;
var buffer = new byte[l];
sr.BaseStream.Read(buffer, 0, l);
outputStream.Write(buffer, 0, l);
}
// creating the document parts
using (WordprocessingDocument docx = WordprocessingDocument.Open(outputStream, true)) {
// adding parts here...
// adding a large amount of data in an iterator
foreach(var item in someList) {
// adding some parts...
outputStream.Flush();
}
docx.Close();
}
outputStream.Flush();
outputStream.Close();
outputStream.Dispose();
我必须说,我正在处理的数据,大约是来自DB的100000条记录
在本地,它工作得很好;在生产服务器(win 2012 R2)中,它可以很好地处理小文件;但在大文件中(当有太多记录无法写入文件时),我会遇到以下错误:
无法创建互斥。(来自HRESULT的异常:0x80131464)
这是堆栈跟踪:
在System.IO.IsolatedStorage.IsolatedStorageFile.Open(String infoFile,String syncName)
位于System.IO.IsolatedStorage.IsolatedStorageFile.Lock(布尔值&锁定)
位于System.IO.IsolatedStorage.IsolatedStorage FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、IsolatedStorage文件isf)
位于MS.Internal.IO.Packaging.PackagingUtilities.SafeIsolatedStorageFileStream..ctor(字符串路径、文件模式、文件访问权限、文件共享、ReliableSolatedStorageFileFolder文件夹)
位于MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount、字符串和文件名)
在MS.Internal.IO.Packaging.SparseMemoryStream.switchModeif-needed()处
在MS.Internal.IO.Packaging.SparseMemoryStream.Write处(字节[]缓冲区,Int32偏移量,Int32计数)
在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.RawText(字符串s)
位于System.Xml.XmlUtf8RawTextWriter.WriteStartAttribute(字符串前缀、字符串localName、字符串ns)
位于System.Xml.XmlWellFormedWriter.WriteStartAttribute(字符串前缀、字符串localName、字符串namespaceName)
位于DocumentFormat.OpenXml.openxmlement.WriteAttributesTo(XmlWriter XmlWriter)
位于DocumentFormat.OpenXml.openxmlement.WriteTo(XmlWriter)
位于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.openxmlement.WriteTo(XmlWriter)
位于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.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.OpenXmlPartRootElement.Save()处
位于DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents()处
位于DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(布尔disposing)
位于DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()处
在MyClass.GetDocx()中
问题是:当我将文件写入我自己的
App_Data
文件夹时,为什么它会成为IsolatedStorage
?如何防止这种情况发生?在堆栈跟踪中可以找到类。这个隐藏的类使用内存流或独立存储。它取决于两个参数,即低水位线和高水位线。如果内存消耗大于高水位线,则使用隔离存储。正如您所观察到的,对于在IIS上运行的服务器端应用程序来说,这会成为一个问题。不幸的是,问题是这些参数的值在类中是硬编码的。换言之,这是出于设计
private const long _lowWaterMark = 0x19000; // we definately would like to keep everythuing under 100 KB in memory
private const long _highWaterMark = 0xA00000; // we would like to keep everything over 10 MB on disk
EPPlus库的作者也有类似的想法,在我看来,解决方案是使用非Microsoft打包库
我不知道openxml sdk,但也许可以用其他东西替换默认的Microsoft打包解决方案。您看到这个问题了吗@MichałKomorowski是的,我看到了(事实上还有很多谷歌文档)。我的问题是,当我在自己的App_数据文件夹中编写docx时,为什么它会
IsolatedStorage
?我怎样才能防止呢?谢谢你的帮助,回答得很好。我曾经和你一样是一个深沉的代码读者;但我似乎老了;)谢谢你的帮助。