17

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.

Gianlucca
  • 1,334
  • 2
  • 14
  • 26

3 Answers3

25

After some research I managed to get it working.

  1. Open your .csproj file

  2. 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>
  1. Move the dll to the root folder of your project (where Program.cs is)

  2. Navigate to the root folder of your project using console/terminal and execute dotnet restore to import all the references

  3. Then, execute dotnet run

  4. 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?)

Gianlucca
  • 1,334
  • 2
  • 14
  • 26
  • I faced a similar issue today with a .net core 2.0 app.. My work-around was to create a new .net framework web app (not .net core) and add the references there. Then I copied the item groups from the csproj into my .net core project's csproj file. That did the trick. – agileMike Oct 18 '17 at 21:45
  • Where you mention add code below I had to add above the tag to get dotnet restore to work, not sure if its something I'm doing wrong. – Yas V Mar 31 '18 at 22:15
  • Thank you, you helped me to fix an issue with a Xamarin project and AppCenter! – Gold.strike Jul 17 '18 at 21:56
1

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

SIbghat
  • 281
  • 3
  • 5
0

Visual Studio Community 2019 [v16.8.4]

  1. Right click your aspnetcore project
  2. Project Reference... > Browse > Browse...
  3. Locate and Add your DLL file

enter image description here enter image description here

SimplyInk
  • 5,832
  • 1
  • 18
  • 27