Today I upgraded my NANT installation to latest Version 0.91 Alpha2. Oh well, this
was quite a mess I had to deal with after that. Here is the first error I ran into when I tried to
compile a .Net 4.0 project.
Unhandled Exception: System.TypeInitializationException: The type initializer for 'NA
nt.Console.ConsoleStub' threw an exception. ---> System.Security.SecurityException: R
equest for the permission of type 'System.Security.Permissions.FileIOPermission, msco
rlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& s
tackMark, Boolean isPermSet)
..
..
at log4net.Core.DefaultRepositorySelector.GetRepository(Assembly repositoryAssembl
y)
at log4net.Core.LoggerManager.GetLogger(Assembly repositoryAssembly, String name)
at log4net.LogManager.GetLogger(Assembly repositoryAssembly, String name)
at log4net.LogManager.GetLogger(Type type)
at NAnt.Console.ConsoleStub..cctor()
I baned my head all over the place on google, bing and what not. There is no clear understanding
of why this is happening and what the fix is. The kicker is that not everybody is
experiencing the issue. So when I read some posts on the development forum, I could not
find any answer because they also are not able to reproduce.
The message very clearly says that it has something to do with Code Access Security
related to somme file permissions. And it is pointing to log4net as origin. I was
having this issue on my Windows 7 machine. I looked at the code fcr log4net as well as
NANT. There was no explicit addition of demand of FileIOPermission policy.
Then I realized that in prior versions of .Net, there used to be tool to configure
Code Acess Policy and it had to do with ZONES. When I looked at properties
of log4net assembly, I saw something that immediately rang a bell. Since the file was
downloaded from internet, the assembly is blocked from performing lot of secure
operations on the machine. Well FileIOPermissions is one of the top most secure policies that
can make or break your system. So I simply clicked on Unblock button and there
you go. Now I got pass that error.
Then I ran into some more policy related errors. Bottom line is that you will have to
Unblock all assemblies in your folder. That should do the trick. Here are some of the errors
that I ran into after fixing first one.
Error creating FileSet. ---> System.Security.SecurityException: Request for the permi
ssion of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.
0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& s
tackMark, Boolean isPermSet)
I hope this information will save you some pain that I had to go through :-)