本文介绍了Visual Studio的单元测试安全例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,监视打印队列的应用程序。要做到这一点,需要能够管理它们。该应用程序运行正常,当我运行调试,但是,当我试图单元测试安全异常类。有没有办法让Visual Studio的单元测试更高的安全级别?

I am working on an application that monitors print queues. To do this, it needs to be able to administrate them. The application runs fine when I run it for debugging, however, when I try to unit test the class a security exception is thrown. Is there a way to give the visual studio unit test a higher security level?

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



抛出异常:由安全透明的方法'MS.Internal.PrintWin32Thunk.PrinterDefaults
尝试.Dispose(布尔)访问安全至关重要法MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDispose(布尔)'失败。

throws the exception:Attempt by security transparent method 'MS.Internal.PrintWin32Thunk.PrinterDefaults.Dispose(Boolean)' to access security critical method 'MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDispose(Boolean)' failed.

此外,没有抛出异常和类的工作单元测试外使用预期。我曾尝试运行Visual Studio作为管理员,但仍接收到异常

Again, no exception is thrown and the class works as expected when used outside of unit testing. I have tried running visual studio as an administrator, however still received the exception.

更新:它看起来像我将无法测试这个类。包含它的类库已经写在.NET 3.5,这就是为什么使用的类时,不会发生异常。我做了一些进一步的实验,结果发现,尽管这个类库目标.NET 3.5的类抛出该异常不能由应用程序针对.NET 4.0中使用。 Visual Studio的测试项目都必须面向.NET 4.0。

UPDATE: It looks like I won't be able to test this class. The class library containing it is already written in .NET 3.5, which is why the exception does not occur when the class is being used. I did some further experimentation and found that even though this class library targets .NET 3.5, the class which is throwing the exception cannot be used by an application targeting .NET 4.0. Visual studio test projects are required to target .NET 4.0.

推荐答案

您的问题是的可能的相关这个已知的错误:

Your problem is probably related to this known bug: http://connect.microsoft.com/VisualStudio/feedback/details/552520/methodaccessexception-when-printqueuecollection-disposed

有上市有两种解决方法,其中之一是针对.NET 3.5,而不是4.0。

There are two workarounds listed there, one of which is to target .NET 3.5 instead of 4.0.

您一定在调试异常没有被终结器线程上抛出?它被设置,当你不状态。你得到的异常,如果你明确地调用Dispose然后调试? 。例如

Are you certain the exception is not thrown on the finalizer thread when you are debugging? You don't state when it is disposed. Do you get the exception if you explicitly call dispose then debug? E.g.

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

这篇关于Visual Studio的单元测试安全例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:34