Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to NuGet packages from csx script

I am trying to write a C# interactive script (.csx) that needs to use a NuGet package, but I must be overlooking something fundamental because I can't get it to work.

I tried adding a project.json that refers to the package, and it gets installed into the global packages dir when I nuget restore, but it seems the #r directive does not look there. I failed to find a relevant documentation on how the #r directive work (most docs seem to deal with the similar but different project called ScriptCS).

How do you write a csx script that references NuGet packages?

like image 473
Krumelur Avatar asked Jan 21 '16 09:01

Krumelur


People also ask

How do I reference a Nupkg file in Visual Studio?

Visual Studio (csproj > Properties > Package > Tick "Generate NuGet Package on Build". Then Build the solution) dotnet CLI (in command prompt in project folder: dotnet build , then dotnet pack commands) NuGet CLI (in command prompt in project folder: dotnet build , then nuget pack command)

What is .CSX file?

Developer file written in Visual C# Script, a C# API developed by Microsoft under the codename The Roslyn Project; used for developing components in C# that can run on-demand as scripts with the Roslyn framework. The Roslyn program that runs CSX files as scripts is named rcsi.exe.

How do I get NuGet packages in Visual Studio 2022?

In Visual Studio, select Tools, and then select Options. Select NuGet Package Manager, and then select Package Sources. Enter the feed's Name and Source URL, and then select the green (+) sign to add a new package source. If you enabled upstream sources in your feed, clear the nuget.org checkbox.


2 Answers

Dotnet Script has support for referencing Nuget packages in CSX files:

#r "nuget: AutoMapper, 6.1.0"

https://discoverdot.net/projects/dotnet-script#nuget-packages-1

like image 61
pwhe23 Avatar answered Oct 05 '22 21:10

pwhe23


Very simple from Visual studio UI:

  1. Right click the project, Click Manage NuGet Packages

enter image description here

  1. Selected the NuGet Package from the list in Browse or Installed tab and click the nuget.org URL

enter image description here

  1. URL will lead to this webpage, Go to Script and Interactive tab, then simply click the copy button and paste it in your CSI in Visual studio or *.csx file

enter image description here

like image 37
Chandraprakash Avatar answered Oct 05 '22 19:10

Chandraprakash