Visual Studiounit testing安全例外

我正在开发一个监视打印队列的应用程序。 要做到这一点,它需要能够管理它们。 当我运行它进行调试时,应用程序运行正常,但是,当我尝试对类进行unit testing时,会抛出安全exception。 有没有办法让visual studiounit testing具有更高的安全级别?

PrintQueue q = new PrintQueue(server, QueueName, PrintSystemDesiredAccess.AdministratePrinter); 

抛出exception:尝试通过安全透明方法’MS.Internal.PrintWin32Thunk.PrinterDefaults.Dispose(Boolean)’来访问安全关键方法’MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDispose(Boolean)’失败。

同样,在unit testing之外使用时,不会抛出任何exception并且类按预期工作。 我曾尝试以管理员身份运行visual studio,但仍然收到exception。

更新:看起来我将无法测试这个类。 包含它的类库已经用.NET 3.5编写,这就是使用该类时不会发生exception的原因。 我做了一些进一步的实验,发现虽然这个类库以.NET 3.5为目标,但是抛出exception的类不能被面向.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。

您确定在调试时终结器线程上没有抛出exception吗? 您没有说明何时处置。 如果你明确地调用dispose然后调试,你会得到exception吗? 例如

 PrintQueue q = new PrintQueue(server, QueueName, PrintSystemDesiredAccess.AdministratePrinter); q.Dispose();