64

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203

3 Answers3

102

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

Community
  • 1
  • 1
AlbertK
  • 11,841
  • 5
  • 40
  • 36
  • 4
    Added a reference that could not be loaded. Rebuilding the project as suggested did it for me – Wouter Vanherck Jun 22 '18 at 13:02
  • Thanks, In my case I am using Azure DevOps and lib folder containing external assemblies was not exist on git. After the copy, it working perfectly. – Nilay Mehta Oct 21 '18 at 11:16
  • How to debug this referenced dll added to the asp.net core project ?. I have a class library targeting 4.7 .net framework and a asp.net core porject both added to the same solution and this asp..net core references this class library dll when running the asp.net core app the break points never get hit .Any idea ? – user581157 Jan 22 '19 at 11:21
  • Debugging should be OK. Try to check that your external `dll` is built in debug mode. Also try to uncheck "Enable Just my Code" `Tools -> Options -> Debugger`. If it doesn't help ask a separate question with details. – AlbertK Jan 22 '19 at 17:31
  • @AlbertK What about dependencies of the MyAssemblies.dll. For e.g. the MyAssemblies depends on MyDependency.dll (Referenced by NuGet package). Will I have to add MyDependency.dll to my .csproj ? For more details about my question please follow https://stackoverflow.com/questions/55417837/system-io-filenotfoundexception-net-core-web-api-cannot-find-child-dependency link. – Kishan Vaishnav Apr 01 '19 at 06:56
  • 4
    I have a .NET Core 3.1 project in Visual Studio 2019, and there is no "Dependencies > Add reference > Browse" option. All I see is "Add project reference", "Add shared project reference", and "Add COM reference". – Dai Apr 17 '21 at 18:16
  • 4
    Stupidly it's now under "Add Project Reference"->Browse – AaronLS Feb 09 '22 at 22:23
11
  • .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.


Community
  • 1
  • 1
Set
  • 47,577
  • 22
  • 132
  • 150
  • 1
    It is weird that I used .net core 1.0, it works fine. Then I update my framework, but the external assembly cannot be referenced. It throws assembly not found exception directly. – Howard Apr 27 '17 at 13:33
  • 4
    What is really inconvenient is that you can do "Add reference" in the C# project, and just point to a .NET Core assembly dll (that you created yourself), and it will reference it fine, and the intellisense will work for it. However the application will not run, because the referenced dll does not load (it's not in the TRUSTED_PLATFORM_ASSEMBLIES, which you will see if you set environment variable COREHOST_TRACE =1). So you spend a whole afternoon figuring out why the VS Add Reference is not working. Thank you MS. – Wout Jan 12 '18 at 15:54
0

You can add a dll with the following code:

[DllImport("MyLbraryFile.dll", SetLastError = true, CharSet = CharSet.Auto)]

All you have to do is put the dll in the same directory.

https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.110).aspx

Leon Husmann
  • 664
  • 1
  • 6
  • 25
  • 5
    Why did you accept this answer? This does not look like an unmanaged DLL. It looks like a .net assembly. And @Leon, why did you decide that this was an unmanaged DLL, and even more surprising, why did you opt for `SetLastError = true`? Looks like you just copied that from somewhere without really thinking about the implications. What makes you believe that this DLL does indeed call the Win32 `SetLastError()` function? – David Heffernan Oct 18 '16 at 14:04
  • 3
    @DavidHeffernan yes your right, i copied this line from my own project. I use a vb.net dll there, thats why i use SetLastError = true. Indeet this is not needet here. And I think that he accept the answer, because it works for his purpose. – Leon Husmann Oct 18 '16 at 14:40
  • 7
    The same directory as what? Solution, project, class files dir? – BillHaggerty Jun 02 '17 at 13:01