我正在开发一个监控打印队列的应用程序。为此,它需要能够管理它们。当我运行它进行调试时,该应用程序运行良好,但是,当我尝试对该类进行单元测试时,抛出了一个安全异常。有没有办法给visual studio单元测试更高的安全级别?
PrintQueue q = new PrintQueue(server, QueueName,
PrintSystemDesiredAccess.AdministratePrinter);
抛出异常: 尝试通过安全透明方法“MS.Internal.PrintWin32Thunk.PrinterDefaults.Dispose(Boolean)”访问安全关键方法“MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDispose(Boolean)”失败。
同样,在单元测试之外使用时,不会抛出异常并且该类按预期工作。我尝试以管理员身份运行 visual studio,但仍然收到异常。
更新:看来我无法测试该类(class)。包含它的类库已经用.NET 3.5编写,这就是为什么在使用该类时不会出现异常的原因。我做了一些进一步的实验,发现即使这个类库以 .NET 3.5 为目标,抛出异常的类也不能被以 .NET 4.0 为目标的应用程序使用。 Visual Studio 测试项目需要面向 .NET 4.0。
最佳答案
您的问题可能与这个已知错误有关:http://connect.microsoft.com/VisualStudio/feedback/details/552520/methodaccessexception-when-printqueuecollection-disposed
此处列出了两种解决方法,其中一种是针对 .NET 3.5 而不是 4.0。
您确定在调试时没有在终结器线程上抛出异常吗?您没有说明何时处置。如果你显式调用 dispose 然后调试,你会得到异常吗?例如
PrintQueue q = new PrintQueue(server, QueueName,
PrintSystemDesiredAccess.AdministratePrinter);
q.Dispose();
关于c# - Visual Studio 单元测试安全异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6635616/