CS-Script 3.20.0

Referencing NuGet packages

Very often assemblies, which are referenced by script can be sourced from NuGet - global repository for the .NET libraries. Prior version v3.8.13 CS-Script users had to download NuGet packages manually and reference extracetd package assemblies explicitly from the script. However the later releases of CS-Script allow automating this task. Thus CS-Script provides the developer experience similar to the offerd by Visual Studio. The NuGet package referencing is implemented with the dedicated script directive //css_nuget

 //css_nuget [-noref] [-ng:<nuget arguments>] <package>[,package];

package - name of the NuGet package to be referenced. It is the official name o the package as it is published on www.nuget.org.
noref - a flag indicating that automatic assemly referencing (after downloading/installing the package) should be suppressed.

ng - List of command line arguments to be passed to the nuget.exe for an individual package.


It is important to understand differences in handling NuGet packages in CS-Script and Visual Studio. These differences are due to the conceptual differences of scripted and compiled execution.

Thus the //css_nuget directive will instruct CS-Script to download the package and extrat (install) it's content into the CS-Script cache directory for NuGet packages (e.g. C:\ProgramData\CS-Script\nuget). The script engine downloads the package only once. Thus if during the script (any script) execution the the the script engine detects dependency on the NuGet package it will check first if th epackage is already available locally and only if it's not downloading from the official NuGet repository will be attempted.

After downloading and extracting (installing) the package into the NuGet cache the script engine analyses the package content and automatically referenecs the all compatible assemblies. Compatible in this cas means "assemblies targeting the same or lower CLR version". This is to avouid double referencing as the package often contain multiple builds of the same assemlies.

Limitations
NuGet directives are not supported on Linux and .NET v1. 

Example  

This is the example of using the DotNetZip library form NuGet.ORG. 

//css_nuget dotnetzip
using Ionic.Zip;
using System;

class Script
{
    static public void Main()
    {
        using(ZipFile zip = new ZipFile())
        {
            zip.AddFile("app.exe");
            zip.AddFile("readme.exe");
            zip.Save("app.zip");
        }
    }
}

 
Note

If for whatever reason you want to avoid automatic referencing then you should use -noref argument and provide the explicit referencing with //css_ref . 

Instead of auto referencing: 

//css_nuget dotnetzip
using Ionic.Zip;
...

Reference assembly explicitly: 

//css_nuget -noref dotnetzip
//css_ref %css_nuget%\dotnetzip\DotNetZip.1.9.3\lib\net20\Ionic.Zip.dll
using Ionic.Zip;
...

See also

Script Engine Directives