Reference External Dll in .Net Core Project

Reference external DLL in .NET Core project

.Net Core 2 supports a direct reference to external .dll (e.g. Net Standard libraries, classic .Net Framework libraries). You can do it through Visual Studio UI: right click on Dependencies->Add reference->Browse and select your external .dll.

Alternatively, you can edit .csproj file:

<ItemGroup>
<Reference Include="MyAssembly">
<HintPath>path\to\MyAssembly.dll</HintPath>
</Reference>
</ItemGroup>

You can face with the following error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly

then just remove \bin folder an rebuild the project. It should fix the issue.

How it is possible

Net Core 2.0 supports .Net Standard 2.0. Net Standard 2.0 provides a compatibility mode to connect .Net Core(.Net Standard) and .NET Framework. It can redirect references e.g. to System.Int32 from mscorlib.dll(Net. Framework) to System.Runtime.dll(Net. Core). But even if your net core app is successfully compiled with dependency on external dll you may still have issues with compatibility during runtime if there is any API used by external library which .Net Standard doesn’t have.

How to add reference to an external dll file in asp.net core project

As of now, you cannot directly add full .NET framework dll into ASP.NET core project (netcoreapp1.0) directly. You will have to create NuGet package.

If it is project specific dll then create local NuGet package. These are the steps we followed in our project to generate NuGet package-

1.Download Nuget.exe and place it in the folder where .csproj file exists.

2.Open cmd and type nuget spec. File with .nuspec extension will be created.

3.Open the created file and add tag:

<files> <file src="..\..\SomeRoot\**\*.*" target="libs\net461" /> </files>

4.Execute nuget pack A.csproj –IncludeReferencedProjects in cmd. File with .nupkg extension gets created.

5.Go to visual studio. In NuGet package manager settings, Add in “Package Sources” and provide path where your .nupkg and .nuspec file exists.

6.Close Nuget package manager and again open it. Now you can find it in your created package source under browse tab.

Note: Your .nuspec file should be like :

<?xml version="1.0"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ABC.XYZ.FL</id>
<version>1.0.0.0</version>
<title>ABC.XYZ.FL</title>
<authors>ABC</authors>
<owners>ABC</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Framework that will be used to create objects in XYZ world</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>2016</copyright>
<tags>ABC.XYZ.FL</tags>
</metadata>
<files>
<file src="bin\Debug\*.dll" target="lib\net461" />
</files>
</package>

The following links contains more details about creating nuget package and hosting it locally:

https://docs.nuget.org/create/creating-and-publishing-a-package

https://docs.nuget.org/create/hosting-your-own-nuget-feeds

https://docs.nuget.org/consume/nuget-config-file

See if this helps.

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

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>

How can I create a DLL for external referencing in .NET?

As suggested by Hans Passant, using DllExport did the trick.

This package relies on inserting the same .export descriptor into the IL files of the targeted project to expose certain methods once the DLL is created. However, this package also takes care of a host of other necessary configuration properties for the targeted .csproj files. As such, simply adding the .export descriptor is not enough when it comes to using .NET and Visual Studio.

It is also important to mention that getting this to work required me to switch from using .NET Core to .NET Standard 2.0 for my Class Library project as I could not get it to work (out of the box) with neither .NET Core nor .NET 6.0.

.NET Core build with dll reference

You can add assembly references using Visual Studio, same as with .NET Framework projects.

If you want to modify the project file by hand, you'll need something like the following:

<ItemGroup>
<Reference Include="MyLibrary">
<HintPath>..\..\..\MyLibrary\out\MyLibrary.1.0.dll</HintPath>
</Reference>
</ItemGroup>

Add reference to assembly .net core

Yes, it is possible, if you add something like this to your csproj:

<ItemGroup>
<Reference Include="RequiredAssembly">
<HintPath>path\to\RequiredAssembly.dll</HintPath>
</Reference>
</ItemGroup>

Be sure that, when building, it is possible to resolve the reference, since compilation errors only when using in the code. The build prints a warning like this, in such a case:

warning MSB3245: Could not resolve this reference. Could not locate the assembly "RequiredAssembly". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

Two good answers related with this:

  • Using external .dll in dot net core
  • How to add external assembly (.dll) to .NET Core 2.0 on Visual Studio Code

However, you could still use a nuget package, even locally - setup a local nuget repository and manage your dependencies using it. It is pretty straightforward :)

These two links walk through the process:

  • https://www.c-sharpcorner.com/article/creating-a-local-nuget-package-repository/
  • https://medium.com/@churi.vibhav/creating-and-using-a-local-nuget-package-repository-9f19475d6af8


Related Topics



Leave a reply



Submit