Saturday 5 November 2011

TIP: Running NAnt 0.91 on Windows 7

Since I've joined a company several months back which is primarily .NET based, I've been working with a recent version of NAnt on 32-bit Windows XP Pro SP3, which I didn't get to install and configure as it was a hand-me-down desktop. While deciding to do a little experimentation with NAnt 0.91 at home on 64-bit Windows 7, I discovered that it wasn't just as easy as downloading it, unzipping it to a directory, putting it on the system path and running it. Naturally, without observing the RTFM rule, I tried the above and encountered two errors. After RTFM, I was still none-the-wiser : ) Here I explain what these command line errors are and how to resolve them. Note that the my Windows 7 UAC (User Account Control) sub-system and et cetera have default settings on my system.

First Error: .NET Framework Related
 

C:\Development\nant-tests>nant
 

NAnt 0.91 (Build 0.91.4312.0; release; 22/10/2011)
Copyright (C) 2001-2011 Gerry Shaw
http://nant.sourceforge.net


INTERNAL ERROR
 

Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
 

    Could not load file or assembly 'System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
 

For more information regarding the cause of the build failure, run the build again in verbose mode.
Please send a bug report (including the version of NAnt you're using) to nant-developers@lists.sourceforge.net

 
C:\Development\nant-tests>


After having a look at the requirements section of the NAnt 0.91 manual, I still found things a little confusing - for example, I thought I had .NET installed as the following directory existed C:\Windows\Microsoft.NET\Framework64\v4.0.30319 and similar for Framework directory for the 32-bit version but this was not the case. Unbeknownst to me, I only had the client profile installed which can be seen by opening up the Programs and Features window in the Control Panel and searching for Microsoft .NET Framework 4 Client Profile. After a bit of online perusing and a pinch of intuition, I found that I needed to download and install the dotNetFx40_Full_x86_x64.exe executable from the Microsoft website and once done, Microsoft .NET Framework 4 Extended appeared under the Programs and Features window in the Control Panel and the above .NET Framework error was resolved.

Second Error: Windows 7 Security Related 


C:\Development\nant-tests>nant
 

log4net:ERROR XmlConfiguratorAttribute: Exception getting ConfigurationFileLocation. Must be able to resolve ConfigurationFileLocation when ConfigFile and ConfigFileExtension properties are not set.
System.Security.SecurityException: Request for the permission 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& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.AppDomainSetup.VerifyDir(String dir, Boolean normalize)
   at log4net.Util.SystemInfo.get_ConfigurationFileLocation()
   at log4net.Config.XmlConfiguratorAttribute.ConfigureFromFile(Assembly sourceAssembly, ILoggerRepository targetRepository)
 

The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.FileIOPermission
The Zone of the assembly that failed was:
Internet

 
Unhandled Exception: System.Security.SecurityException: Request for ConfigurationPermission failed while attempting to access configuration section 'nant'. To allow all callers to access the data for this section, set section attribute 'requirePermission' equal 'false' in the configuration file where this section is declared. ---> System.Security.SecurityException: Request for the permission of type 'System.Configuration.ConfigurationPermission, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.Configuration.BaseConfigurationRecord.CheckPermissionAllowed(String configKey, Boolean requirePermission, Boolean isTrustedWithoutAptca)
   --- End of inner exception stack trace ---
   at System.Configuration.BaseConfigurationRecord.CheckPermissionAllowed(String configKey, Boolean requirePermission, Boolean isTrustedWithoutAptca)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at NAnt.Console.ConsoleStub.Framework.GetRuntimeFramework()
   at NAnt.Console.ConsoleStub.Main(String[] args
 

C:\Development\nant-tests> 

In addition to the above being spat out into the command line, Windows 7 will pop up a window to let you know that NAnt has stopped workingIf you click on the View problem details link, you can see that one of the Problem Signatures reads System.Security.Security - this is a clue as to what the issue is. After searching online again, I found that the problem was Windows 7 security related in that the downloaded NAnt 0.91 zip file needed additional security related configuration to be performed: before extracting, one must right click on the zip file, select Properties and under the General tab, click the button labelled Unblock, then click OK on the Properties window. Now, extract the file to your desired location, ensure it is on the system path, open a new command line and NAnt should run successfully.

Note that when using NAnt Contrib tasks with the loadtasks task, ensure that the zip which the Contrib tasks was extracted from was unblocked as above. If not, either delete the current Contrib directory, unblocking the Contrib zip and extracting again or configure the the DLL used in loadtasks by unblocking it separately, just like the zip.