How do I add assembly references in Visual Studio Code?
.csproj Project fileThe 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
ExampleAdd Newtonsoft.Json
NuGet package to a project:
dotnet add package Newtonsoft.Json
.json Project fileThe 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.
VS Code - The reference assemblies for ".NETFramework,Version=v5.0" were not found
I struggled with this problem for a long time. Here is a list of things that need to be checked.
Remove all SDKs from your computer. - Find in the control panel "Programs and Features" then delete all
Microsoft .NET Runtime - x.x.xx
,Microsoft .NET SDK x.x.x
Install newest .NET SDK and Runtime - i use this site
Check your PATH - Check if the path with the environment variables in windows contains
C:\Program Files\dotnet
!! Check MSBuildSDKsPath Environment Variable - It turned out that installing the new SDK does not change the information in the environment paths. I still had the old version listed there and after uninstalling everything, I got a message saying that
'Microsoft.NET.Sdk.Web' could not be found.
Check the value of the MSBuildSDKsPath variable. The proper value for me was x
C:\Program Files\dotnet\sdk\5.0.401\Sdks
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 create a Reference Assembly with Visual Studio 2017
How to create a Reference Assembly with Visual Studio
Workaround:
Try using ProduceReferenceAssembly
when you're in VS2017. Then you can find the reference assembly in ref
folder. (Or you can use VS2019, this issue is fixed there.)
According to this document, ProduceOnlyReferenceAssembly
is a boolean value that instructs the compiler to emit only a reference assembly rather than compiled code. This property corresponds to the /refonly
switch of the vbc.exe and csc.exe
compilers.
It's strange that the ProduceOnlyReferenceAssembly
failed to work in VS while the command-line worked. I think it could be one issue with VS2017, so I suggest you report this one to DC forum.
(I test the property and find it works well in VS2019).
Related Topics
How to Save/Download Pdf Embedded in Web Page Without a Pdf Filename
Linq to Find All Childs Related to Parents Id
Removing Elements from Json Based on a Condition in C#
Calling a Function from Another Class in C#/.Net MVC App
How to Call a Method Daily, At Specific Time, in C#
How to Iterate Through the Following Json Using C#
How to Check If a Datetime Value Is Empty or Not in a Put Request
How to Check If User Input Is from Barcode Scanner or Keyboard
Truncate Two Decimal Places Without Rounding
Client Specific Role Based Authentication
Converting Datetime C# Type to Date JavaScript in ASP.NET MVC Razor Application
Declare Properties to Ignore in Entities Interface (Ef Core)
How to Secure the Asp.Net_Sessionid Cookie
Automatic Enhancement of Scanned Images
How to Use a Class from One C# Project With Another C# Project
Linq: Select an Object and Change Some Properties Without Creating a New Object