值不能为空。参数名称:目标

Value cannot be null. Parameter name: dest

我有一个小应用程序可以将一些文本数据写入压缩文本文件。它适用于第一个存档,但一段时间后我收到以下未处理的异常:

System.ArgumentNullException: Value cannot be null.
Parameter name: dest

我添加了 && line.Lenght >0,因为我认为这可能是问题所在,但没有任何改变。 我在 3 小时内获得 1 个完整存档,有时是第二个存档的一半,后者较小,我想它还没有完成,但已写入磁盘,扩展名为 .tmp。我可以将其重命名为 zip 和 open/unpack/read 数据。 如果我将 "counter" 更改为更小的周期,例如10 分钟,它写了一堆档案,好像一切都很好。足以让我认为错误已经消失了。 所以这个错误可能会在大约 4-5 小时后出现。 如何解决这个问题?

堆栈跟踪:

[ERROR] FATAL UNHANDLED EXCEPTION: 
Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: dest
  at System.IO.MemoryStream.set_Capacity (System.Int32 value)
  at System.IO.MemoryStream.EnsureCapacity (System.Int32 value)
  at System.IO.MemoryStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at SharpCompress.IO.NonDisposingStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at System.IO.Compression.ZipArchiveEntryStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder)
  at System.IO.StreamWriter.Write (System.Char[] buffer, System.Int32 index, System.Int32 count)
  at System.IO.TextWriter.WriteLine (System.String value)
  at Program.DataWriter ()
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state)
  at System.Threading.ThreadHelper.ThreadStart () 

代码如下:

static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();

static void Main(string[] args)
{
    Thread thread = new Thread(DataWriter);
    thread.Start();
}

static void DataWriter()
{
    while (true)
    {
        string start = DateTime.Now.ToString("yy-MM-dd-HH-mm-ss");
        DateTime counter = DateTime.Now.AddHours(3);

        using (var archive = ZipFile.Open(HistoryPath + "history-" + start + ".tmp", ZipArchiveMode.Update))
        {
            ZipArchiveEntry data = archive.CreateEntry("data-" + start + ".txt");
            using (StreamWriter writer = new StreamWriter(data.Open()))
            {
                while (DateTime.Now < counter)
                {
                    if (queue.TryDequeue(out string line))
                    {
                        if (line != null && line.Length > 0)
                        {
                            writer.WriteLine(line);
                        }
                    }

                    System.Threading.Thread.Sleep(10);
                }
            }
        }

        File.Move(HistoryPath + "history-" + start + ".tmp", HistoryPath + "history-" + start + "_"+ DateTime.Now.ToString("yy-MM-dd-HH-mm-ss") + ".zip");
    }
}

更改 ZIP 库解决了我的问题。我已将 Microsoft System.IO.Compression 更改为 ICSharpCode.SharpZipLib。代码几乎是一样的。现在一切正常。微软库似乎有错误。