C# 这条小溪没有开放供阅读
我正在从云下载PowerPoint文件。为了限制下载文件的多次调用,我使用C# 这条小溪没有开放供阅读,c#,.net,caching,memorycache,C#,.net,Caching,Memorycache,我正在从云下载PowerPoint文件。为了限制下载文件的多次调用,我使用MemoryCache缓存它。以下代码在第一次运行时运行良好,但在第一次运行后会引发异常: “该流未打开以供阅读。” 下面是我正在使用的代码: var cache = MemoryCache.Default; var memoryStream = new MemoryStream(); if(cache.contains(key)) { memoryStream = (MemoryStream)cache.Get(
MemoryCache
缓存它。以下代码在第一次运行时运行良好,但在第一次运行后会引发异常:
“该流未打开以供阅读。”
下面是我正在使用的代码:
var cache = MemoryCache.Default;
var memoryStream = new MemoryStream();
if(cache.contains(key)) {
memoryStream = (MemoryStream)cache.Get(key);
} else {
var file = getFile();
cache.Add(key, file);
memoryStream = (MemoryStream)cache.Get(key);
}
using(memoryStream) {
Open file and do manupulation then save the file in cloud.
}
更新代码:
var cache = MemoryCache.Default;
byte[] data = (byte[])cache.Get(key);
if(data == null)
{
data_stream = getData(); // get memorystream
cache.Add(key, data_stream.ToArray(), new CacheItemPolicy());
data = (byte[])cache.Get(key);
}
using(Stream outputStream = new MemoryStream(decktemplate)) {
Open file and do manupulation then save the file in cloud.
}
仍然收到以下错误:“算术运算导致溢出。”
下面是堆栈树:
at MS.Internal.IO.Zip.ZipIOExtraField.ParseRecord(BinaryReader reader, ZipIOZip64ExtraFieldUsage zip64extraFieldUsage, UInt16 expectedExtraFieldSize)
at MS.Internal.IO.Zip.ZipIOCentralDirectoryFileHeader.ParseRecord(BinaryReader reader, Encoding encoding)
at MS.Internal.IO.Zip.ZipIOCentralDirectoryBlock.ParseRecord(BinaryReader reader, Int64 centralDirectoryOffset, Int32 centralDirectoryCount, Int64 expectedCentralDirectorySize)
at MS.Internal.IO.Zip.ZipIOCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
at MS.Internal.IO.Zip.ZipIOBlockManager.get_CentralDirectoryBlock()
at MS.Internal.IO.Zip.ZipArchive.GetFiles()
at System.IO.Packaging.ZipPackage.ContentTypeHelper..ctor(ZipArchive zipArchive, IgnoredItemHelper ignoredItemHelper)
at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(Stream stream, Boolean readWriteMode)
at DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(Stream stream, Boolean isEditable, OpenSettings openSettings)
at DocumentFormat.OpenXml.Packaging.PresentationDocument.Open(Stream stream, Boolean isEditable)
将
MemoryStream
存储为缓存中的对象是一个非常糟糕的主意,因为多个线程可能同时访问它。更好的办法是将有效负载存储为字节[]
或数组片段
,并使用它为每个调用方创建不同的内存流
。否则,你会经常被自己绊倒:
byte[]有效负载=(byte[])缓存。获取(键);
如果(有效负载为空)
{
有效载荷=GetFilePayload();
cache.Add(key,payload);
}
使用(var memoryStream=new memoryStream(有效负载,可写:false))
{
// ...
}
因为它位于using
块中,所以会被释放。此外,您可能需要在使用后将其倒带(它不是VHS,但有一个指向其所在位置的内部指针)。此外,您是否总是在内存中缓存(序列化流在概念上很难)?但我仍然收到相同的错误:“流未打开以进行读取。”当将MemoryStream的可写标记为true时,它给出了一个异常:“算术运算导致溢出”听起来这两个都不是来自所示的代码;你有堆栈跟踪吗?@ManjurAnsari具体来说,第一个听起来像是getFile
方法中不正确的文件处理代码,第二个听起来像是“and do manufulation”代码中不正确的数据处理代码。您还没有向我们展示这两种方法中的任何一种。getFile方法正在返回MemoryStream。实际上,文件是.pptx。在操作过程中,我打开了.pptx文件和额外的幻灯片,并将最后一组幻灯片上载到cloud中。@ManjurAnsari该语句中的任何内容都不能让我假设代码是正确的