EnvironmentPermission Deny is obsolete in .Net 4.0 any alternate?
Hi I have a TestMethod in .Net 3.5 where I use EnvironmentPermission.Deny() to simulate a scenario to test the condition when access to the environment variables is denied.
Deny() has been made obsolete in .Net 4.0 and it gives compile time error.
How do I write a test for this scenario in .Net 4.0 ?
I'm no expert on this, but I've tried to find some resources that might guide you.
According to the MSDN documentation, as you pointed out, EnvironmentPermission.Deny()
is obsolete in .NET 4.0. The recommended approach is to set your assembly's permission set in order to control what your code can/cannot access.
Here's some links to help you (hopefully)
- http://msdn.microsoft.com/en-us/library/system.reflection.assembly.permissionset.aspx
- http://www.dotnetfunda.com/tutorials/videos/x86-what-is-cas-evidence-permission-set--code-groups-.aspx
- http://www.simple-talk.com/dotnet/.net-framework/whats-new-in-code-access-security-in-.net-framework-4.0---part-i/
I think I have might just found a solution (although it still uses deprecated APIs):
Add the following to your app.config:
<?xml version="1.0"?>
<configuration>
<runtime>
<NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>
</configuration>
Now you should be able to call Permission.Deny()
without incurring a runtime exception.
精彩评论