How do I add assembly references in Visual Studio Code?
.csproj Project file
The following topic applies to .csproj
project file and : .NET Core 1.x SDK, .NET Core 2.x SDK
Adds a package reference to a project file.
dotnet add package
Example
Add Newtonsoft.Json
NuGet package to a project:
dotnet add package Newtonsoft.Json
.json Project file
The following topic applies to .json
project file:
This guide walks you through the process of adding any assembly reference in Visual Studio Code. In this example, we are adding the assembly reference System.Data.SqlClient into .NET Core C# console application.
Note
- At step #6, enter the assembly reference that you want.
- Some assembly reference is applicable to .NET Framework and it will gives you error(s).
- OleDb is not available in .NET Core, probably because it's not cross platform.
Prerequisites
- Install Visual Studio Code
- Install .NET Core SDK (Preview 2 version)
- Install NuGet Package Manager from the Visual Studio Code Extension Marketplace
- Install C# extension from Visual Studio Code Extension Marketplace
Steps
- Launch Visual Studio Code
- Open your project folder
- Launch VS Code Command Palette by pressing F1 or Ctrl+Shift+P or Menu Bar > View > Command Palette
- In Command Palette box, type nu
Click on NuGet Package Manager: Add Package
Enter package filter e.g. system.data (Enter your assembly reference here)
- Press Enter
- Click on System.Data.SqlClient
- The following prompt pops up
- Click on Restore
- The following Output panel pops up
- In the Explorer panel, click on project.json to open it
- In the Editor panel, it shows the assembly reference added into project.json file
- Assembly reference, System.Data.SqlClient used in Program.cs
How to reference assemblies using Visual Studio Code?
The new .NET Core SDK restore command is dotnet restore
To add any asssembly reference in Visual Studio Code, please refer to my post.
Adding reference to another project from visual studio code
Let's say you have two projects:
1) Project1.Api
2) Project2.Executable
Command line syntax for dotnet add reference
:
cd Project2.Executable
dotnet add reference ../Project1.Api/Project1.Api.csproj
If you check the Project2.Executable.csproj file, you will see the following entry:
<ItemGroup>
<ProjectReference Include = "..\Project1.Api\Project1.Api.csproj" />
</ItemGroup>
Can't reference dll assembly to the Visual Studio Code project
You can use dotnet-add reference
CLI command which provides a convenient option to add project references to a project.
Example: Add a project reference:
dotnet add app/app.csproj reference lib/lib.csproj
For more information, refer this.
How to add DLL reference in Visual Studio Code
You need to add a reference to any .dll files in your csTemp.csproj file as such:
<ItemGroup>
<Reference Include="OpenHardwareMonitorLib">
<HintPath>**path\to\your\dll**</HintPath>
</Reference>
</ItemGroup>
VSCode you must add a reference to assembly 'netstandard' but compiles fine
Thanks to @Martin Ullrich and for a good question to @Chris Kooken. I think it should be an approved answer in here for the next visitors to the page.
Following these steps helped me to solve the issue:
* Go to the VSCode settings (File > Preferences > Settings).
* Search with the keywords omnisharp use global mono to locate the configuration.
* Change the value to never from either auto or always.
* REMEMBER to restart the VSCode to activate the change (I restarted it twice to be sure).
Related Topics
Is Task.Run Considered Bad Practice in an Asp .Net MVC Web Application
String Concatenation Using '+' Operator
Has Foreach's Use of Variables Been Changed in C# 5
Open Image from File, Then Release Lock
Getfiles with Multiple Extensions
C# Using Reflection to Copy Base Class Properties
How to Correctly Cast a Class to an Abstract Class When Using Type Generics
Frombody String Parameter Is Giving Null
Method Call Using Ternary Operator
Regex Match Multiple Times in String
Releasing a Unplugged Virtual Serial Port
Return Value from SQL Server Insert Command Using C#
Why Some Types Do Not Have Literal Modifiers
Routing in ASP.NET MVC, Showing Username in Url
How to Draw Line and Select It in Panel
Get the Serial Number of Usb Storage Devices in .Net Core 2.1