本文介绍了LibTiff.NET ReadDirectory是给System.ObjectDisposedException只在单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑: FYI为未来的读者,这一问题已得到修复为BitMiracle的LibTiff.NET版本2.3.606.0的

FYI for future readers, this issue has been fixed as of version 2.3.606.0 of BitMiracle's LibTiff.NET.

我在我的C#库使用BitMiracle的LibTiff.NET(版本2.3.605.0及以下)(编译在.NET 3.5 | 86)继续当我打电话让此异常 ReadDirectory System.ObjectDisposedException:无法写入一个封闭的TextWriter

I'm using BitMiracle's LibTiff.NET (version 2.3.605.0 and below) in my C# library (compiled at .NET 3.5 | x86) and keep getting this exception when I call ReadDirectory: System.ObjectDisposedException: Cannot write to a closed TextWriter

我意识到,这似乎表明,我已经做了呼叫之前已经部署了我的形象的......但我没有特别这样做。这是在库中的错误还是我真的失去了一些东西?

I realize that this seems to indicate that I have already disposed of my image before making the call...but I have not specifically done so. Is this a bug in the library or am I really missing something here?

下面是我的code:

    public static bool IsTiffBiTonal(String tiffFilePath)
    {
        VerifyFileExistence(tiffFilePath);

        using (Tiff tiff = Tiff.Open(tiffFilePath, "r"))
        {
            do
            {
                if (tiff.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt() == 1)
                {
                    continue;
                }
                return false;
            }
            while (tiff.ReadDirectory()); //Error occurs here
        }
        return true;
    }

编辑:确定,我有更多的信息,经过一些进一步的测试,这是只有当我跑步时我的单元测试发生!不知道为什么这样做,虽然改变什么。

Ok, I have more information after some further testing, this is only happening when I'm running my unit tests! Don't know why that would change anything though.

推荐答案

由于谈论单元测试,并让试图写入控制台时同样的错误(ObjectDisposedException当输出到控制台)我意识到LibTiff.NET库试图写入错误控制台。细算通源$ C ​​$ C,我发现这code:

Because of other threads talking about unit testing and getting this same error when trying to write to the console (ObjectDisposedException when outputting to console) I realized that the LibTiff.NET library was trying to write to the error console. After looking through the source code, I found that this code:

        using (TextWriter stderr = Console.Error)
        {
            ...
        }

由于他们被包裹所有的写入错误出在使用,这是第一次写入错误出来后处置Console.Error对象。这对第二次引起了我的错误左右(ReadDirectory做什么上链表调用下一步做)。所以我删除了使用和问题得到了解决!

Because they were wrapping all of the writes to the error out in a using, it was disposing of the Console.Error object after the first write to the error out. This caused my error on the second time around (ReadDirectory does what calling Next on a linked list does). So I removed the using and the problem was fixed!

        TextWriter stderr = Console.Error;
        ...

所以,这里的教训:不要将你的标准输出:)

So, the lesson here: don't dispose of your standard outputs :)

我就问他们为什么以往任何时候都允许在单元测试配置标准输出而不是在这里等情况下,另一个问题:.NET - 为什么处置标准输出的过程中单元测试只允许 ?。如果您有任何问题的答案......请张贴在那里。

I've asked another question regarding why they were ever allowed to dispose of the standard output in unit tests but not in other situations here: .NET - Why is disposing of standard output only allowed during unit tests?. If you have any answers to the question...please post it there.

这篇关于LibTiff.NET ReadDirectory是给System.ObjectDisposedException只在单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:46