nuget on mono

The common usage of NuGet is a Visual Studio extension that makes it easy to install and update third-party libraries and tools in Visual Studio. But Visual Studio only run natively in windows (other OS need emulator layer or virtualization to make it running).

Nuget is useful in Linux server where MVC sites are deployed or used as build server for deployed Desktop Application and we used nuget package as third party library. Usually we include nuget package in version control or include any dll in bin folder of MVC. Nuget package has lot of unnecessary files to include in version control.

Using nuget in server and retrieve or restore nuget package in application is the preferable solution. But, only command line version of nuget is can be used in server.

First, Download NuGet command line (NuGet.exe) at nuget.codeplex.com
it will download NuGet.exe (bootstarp version)

Update: I’m using my own compiled mono which reside in /opt/mono, it may be different to your mono installation. Removed the path.

if we try to execute
# mono NuGet.exe

its will throw errors

Unhandled Exception: System.Net.WebException: Error getting response stream (Trust failure): TrustFailure
---> System.IO.IOException: The authentication or decryption has failed.
---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received form server.

its happen because nuget download nuget from https location and as default mono not trust any https certificate, in order to handle https download issue in mono we need to do:
# mozroots --import --sync

After completes, and we try to execute again. its throw another error:

WARNING: The runtime version supported by this application is unavailable.
Using default runtime: v2.0.50727
Missing method .ctor in assembly /opt/src/nuget-bin/NuGet.exe, type System.Runtime.Versioning.TargetFrameworkAttribute
Can't find custom attr constructor image: /opt/src/nuget-bin/NuGet.exe mtoken: 0x0a000787

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'NuGet'.
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'NuGet'.

there error raised because default mono uses v 2.0 runtime but nuget ist using v4.0. its can be resolve by define runtime parameter on mono:
mono --runtime=v4.0.30319 NuGet.exe

When we execute again it will successful and download latest NuGet.exe

Additional step to make nuget as script:

put NuGet.exe in mono bin /opt/mono/bin/NuGet.exe
create nuget shell script in /opt/mono/bin/nuget

vi /opt/mono/bin/nuget

#!/bin/sh
exec /opt/mono/bin/mono --runtime=v4.0.30319 --gc=sgen /opt/src/nuget-bin/NuGet.exe "$@"

chmod +x /opt/mono/bin/nuget

how to uses
nuget install Dapper.Rainbow.MySql -Version 0.1.4

it will download require packages to current directory

Posted in Tools | Tagged , , , | 24 Comments