Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference external DLL in .NET Core project

Tags:

c#

.net-core

I've my own .dll file that I used to use with Edge.js in nodejs, I'm trying to use it now with dot net core app, but found no where/no how to get access to it, or define it.

is there something like

"files": {     "":"MyLibrary.dll" } 

or like

using MyLibraryFile.dll 

so that i can use the functions in it?

my assembly file structure is: MyLibraryFile.dll

namespace MyLibrary {     public class Inventory     {         public async Task<object> Invoke(object input)     { } 

and neither using MyLbrary; nor using MyLbraryFile;worked

I need to use this with MS Code editor, not with MS Studio. and do not want to use NuGet package

like image 261
Hasan A Yousef Avatar asked Oct 18 '16 12:10

Hasan A Yousef


People also ask

How do I add a reference to a dotnet core project?

One method of adding references to your library is by typing it directly in the project. json file. As you can see that we have added some references under the dependencies section as shown in the following code. Let us now save this file and you will see that references are added to your library now.


2 Answers

.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.

like image 85
AlbertK Avatar answered Sep 22 '22 02:09

AlbertK


  • .NET Core works with dependencies only via Nuget. How do I import a .NET Core project to another .NET Core project in Visual Studio? and Referencing standard dlls from a .NET Core XUnit project related.

  • Using VS Code you can add references to Nuget package modifying project.json file. Look into "dependencies" section

    An object that defines the package dependencies of the project, each key of this object is the name of a package and each value contains versioning information. For more information, see the Dependency resolution article on the NuGet documentation site.

    Update: Starting from .NET Core 1.1, you need to modify .csproj file by adding <PackageReference> section. As example:

    <ItemGroup>  <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />  <PackageReference Include="MySql.Data" Version="6.9.9" /> </ItemGroup> 
  • In C# using add namespace, not reference to assembly.


like image 31
Set Avatar answered Sep 22 '22 02:09

Set