How do I add assembly references in Visual Studio Code? - c#

So I've come across a similar issue twice now while working on my first project in C#. When trying to add either using System.Data; or using System.Timers;, I get the following error:
The type or namespace name 'x' doesn't exist in the namespace 'System' (are you missing an assembly reference?).
I have tried beginning a new project and running restore to see if I had accidentally removed something in the dependencies, but upon generating a new project I still receive the same error. I have tried to research the question and have seen answers referring to the 'solutions explorer', but as far as I can see there doesn't seem to be such a feature by this name in Visual Studio Code 1.8.
Can anyone point me in the right direction for how to get these working, perhaps by manually adding into the dependencies?

.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

Use the command dotnet add package to add a package reference to your project. For example: dotnet add package Newtonsoft.Json, which adds the package reference to the *.csproj project file:
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
and now you can run the command dotnet restore to restores the dependencies of your project.
Reference: dotnet add package

drag the dll file and drop it into the bin folder

Above answer from ikolim doesnt work as indicated by someone else too, there is no, Nuget: Install/Reference command. There is only Add Package! So the answer in the below link solved my problem. Manually editing the Myproject.csproj file.
Duplicate of this thread

I've stored the files in a project folder named "dlls" and added the reference files in my .csproj file like this:
<ItemGroup>
<Reference Include="Microsoft.Office.Client.Policy.Portable">
<HintPath>dlls\Microsoft.Office.Client.Policy.Portable.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Office.Client.TranslationServices.Portable">
<HintPath>dlls\Microsoft.Office.Client.TranslationServices.Portable.dll</HintPath>
</Reference>
</ItemGroup>

In case of extisting .dll reference,
Right click project
Add existing item > select path to .dll
After added dll in project,right click .dll
build-action = Content, Copy-to-output-dir = Always/ or if newer

Related

Missing assembly reference - ensure VS creates a .config file for my nuget package

I've created a nuget package including just 1 .dll.
To test installation of the .nupkg, I
create a C# console application in VS
add a local feed pointing to my package directory
install my package from there
start typing in main...
The problem is, if I do this:
using System;
using MyNamespace;
namespace tester
{
class Program
{
static void Main(string[] args)
{
MyClass.MyStaticMethod()
Console.WriteLine("Hello World!");
}
}
}
the line MyClass.MyStaticMethod() says I'm missing an assembly reference, and using MyNamespace; is an unnecessary declaration.
Reading around, this seems to originate from the fact that my nuget package installs to the global packages location: %Current_User%.nuget.\packages, and the .csproj file includes:
<ItemGroup>
<PackageReference Include="MyPackage" Version="1.0.0" />
</ItemGroup>
Is there a way I can ensure all consumers of my package create a packages.config file, and not install my package to the global packages location?
Can I specify this in the .nuspec file somewhere?
The packages.config way is basically the classic (read: "old") way to reference NuGet packages and has since been superseeded by the PackageReference way of referencing packages. You should make sure your package is compatible with PackageReference.
If you don't increase the version number of your package every time you build it and want to try it out in a project, it won't see the update.
Either increase the version or clear your local NuGet packages cache using dotnet nuget locals clear all.
If the DLL is still not being referenced (check the dependencies node that VS shows you in the solution explorer), then your package may not contain the DLL in the expected place (e.g. lib\netstandard2.0 folder if your source project was a .NET Standard 2.0 library)

What is wrong with this code. Why can't I use SqlConnection?

I am 100% newbie to SQl and wanted to make a ConsoleApp with the use of database. I read some about it and tried. When I needed to make SqlConnection, my VS 2019 Preview showed me this
Severity Code Description Project File Line Suppression State
Error
CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'.
This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12
Active
i don't get why it doesn't work
Here's my code
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
SqlConnection cnn;
}
}
}
If you just updated EntityFrameworkCore from version 2.x to 3.x and you're running into this, change your using statement to Microsoft.Data.SqlClient instead of System.Data.SqlClient.
If you're using EntityFrameworkCore.SqlServer it already has that as a dependency, so you shouldn't need to install it explicitly.
This Microsoft blog explains the change.
Assuming that you're using .NET Core - just add NuGet package: System.Data.SqlClient
Your .csproj might look similar to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
</Project>
Most likely the System.Data.SqlClient.DLL in not in the Bin folder, and you have not set reference to the DLL in the project. You have missed the database choice when You install visual studio. And now You just manually add references named "System.Data.SqlClient".
1.right click you project name and choose nuget package options.
2.search "System.Data.SqlClient" and install it.
Hope this is fix Your Error
The specific way to fix this from within VS Code is to
Open a terminal by going to Terminal -> New Terminal
Run dotnet add package System.Data.SqlClient
Run dotnet restore
That last command might not be necessary, but doing this made it work for me. It seems that console app templates don't come prepared for a reference to SqlClient.
Update
I think moving forward projects should use Microsoft.Data.SqlClient and not System.Data.SqlClient: https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/.
As #Mmm mentioned in the comments if you are using .NET Core and have already installed the System.Data.SqlClient package, closing and reopening the project resolved the issue for me too.
I had that error today and what's happening its that VS CODE its no recognizing System.Data.SqlClient;, that's why you can't call the variable...
How did I fix it??
I installed:
SQL server compact toolbox
Nuget Package version updater
Close visual studio/Open it again and your program should be rocking and popping!!
[!["Manage Nuget Package for Solution"]
it fixed my issue.
[1]: https://i.stack.imgur.com/hpk2e.png
I just installed nuget package from the "Manage Nuget Package for Solution". Previously I had used through command but that did not work so I used the UI to install this package.
NuGet Package
Install-Package System.Data.SqlClient
solve my problem.
For me System.Data.SqlClient was already installed. What i had to do was, I went to the Nuget package manager and downgrade the version to 4.8.2 from 4.8.3 and it worked.
It's pretty simple
Go to solution explorer > right click & click manage NuGet packages > and install System.data.SqlClient 4.5.1. or later.
Click here to screenshot

How to add external assembly (.dll) to .NET Core 2.0 on Visual Studio Code

I faced some issues regarding adding an external assembly (.dll) to my .NET Core 2.0 console application on Visual Studio Code as there are little to none documentation about how you can do it.
Microsoft provides a very good tutorial about how one can add NuGet Packages and Project to project references, but no information on how to add external dlls.
After some research I managed to get it working.
Open your .csproj file
Below </PropertyGroup> tag, add
<ItemGroup>
<Reference Include="Your dll file name">
<HintPath>Your dll file name.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<!-- You may set it to true if your dll has a specific version -->
</Reference>
</ItemGroup>
Move the dll to the root folder of your project (where Program.cs is)
Navigate to the root folder of your project using console/terminal and execute dotnet restore to import all the references
Then, execute dotnet run
Do not remove the dll from your root folder. If you do, you will receive the following error:
error CS0246: The type or namespace name 'Your dll File' could not be found (are you missing a using directive or an assembly reference?)
Right click project
Add existing item > select path to .dll
After added dll in project,right click .dll
build-action = Content, Copy-to-output-dir = Always/ or if newer
Visual Studio Community 2019 [v16.8.4]
Right click your aspnetcore project
Project Reference... > Browse > Browse...
Locate and Add your DLL file

Nuget assembly not inside a framework folder

I am using NuGetPackageExplorer to publish a nuGet package on our private nuget feed. Before publishing I used menu option Tools->Analyze Package and getting a warning "Assembly not inside a framework folder".
(Note: I also got an error related to lib folder earlier but corrected it by adding lib folder as suggested by NuGetPackageExplorer)
Following is my nuGet package structure:
->lib
MyDll.Dll
I have not mentioned any specific framework in the Package Metadata.
(I also tried NuGet Assembly outside lib folder, but it is talking about "lib" folder)
Does anyone know a solution to this?
If you go through the source-code of nuget, you will find that this error can be ignored if your dll is targeted for multi-frameworks.
One unit test for nuget core seems to prove this.
I finally found how to do this.
In NuGetPackageExplorer, point to Package Contents -> "lib" folder, if you right click on it (lib folder) then a menu appears and that menu has option to add Framework folder.

Add project.json package references to a VSIX

When trying to add references to a VSIX, it normally pulls it from the references in the .csproj. However, if the references are not in the .csproj, because they now are in a project.json file, then they don't get pulled to the vsix. The solution then may compile, but then the extension fails with "file not found" errors when installed into Visual Studio (since the assemblies where not copied to the VSIX).
I tried with the section of the manifest like so:
<Asset Type="Microsoft.VisualStudio.Assembly" d:Source="Project" d:ProjectName="*PROJECTNAME*" Path="|*ASSEMBLYNAME*|" AssemblyName="|*ASSEMBLYNAME*;AssemblyName|" />
But it does not work, as it does not recognize the package references.
After some research I saw a similar issue with a PCL, however, without an answer and not the same type of problem: MEF With Portable Class library using Microsoft Composition MEF2 throws file not found exception
In the same note, this seems like an acceptable workaround: VSIX with Project Templates and NuGet Packages however, as far as I understood, it implies using the package during the installation. Besides that, it doesn't work for our case as they need to specify the package version and we are using project.json so we can use floating versions (ie: 2.0.*)
Is there a way to reference this project.json references that we are missing? Maybe a workaround? The solutions I have found seem to all require to "paste" de DLL somewhere, which for floating versions is not that convenient.
Thanks in advance for any help or input.
Edit/Update: Since VSIX automatically pushes any assembly referenced in the CSPROJ (and not the project itself), trying to get the DLLs at a project level seems unlikely. After many tries, I think that a valid workaround would be to get the assemblies from the Output Folder. However, to my knowledge, VSIX does not have a way of doing this, or does it?
I'm not sure I'm understanding your question correctly, but if you're trying to install a Project Template via a VSIX and you want the project template to include all it's nuget packages when you use it you could do something like this.
Edit your Project Template's xproj file and add the following lines:
<ItemGroup>
<None Include="project.json"/>
<None Include="project.lock.json"/>
</ItemGroup>
Edit your Project Template's vstemplate file and add the following lines in the Project node:
<ProjectItem ReplaceParameters="true" TargetFileName="project.json">project.json</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="project.lock.json">project.lock.json</ProjectItem>
That should be all you need to do. Now when you install the project template, then create a new project using that template it should include all the nuget packages that were in the project.json file.

Categories

Resources