开发者

Visual Studio Unit Test Security Exception

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);

throws the exception: Attempt by security transparent method 'MS.Internal.PrintWin32Thunk.PrinterDefaults.Dispose(Boolean)' to access security critical method 'MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDisp开发者_高级运维ose(Boolean)' failed.

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.

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

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

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜