1

I'm writing a class library to abstract our company's Database functions, but when the code gets to instantiating one of my database objects we get a:

FileNotFound Exception:

Could not load file or assembly 'MySql.Data, Version=8.0.13.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.

The MySql.Data Dependencies as stated on the website are:

  • .NETStandard 2.0 Google.Protobuf (>= 3.5.1)
  • System.Configuration.ConfigurationManager (>= 4.4.1)
  • System.Security.Permissions (>= 4.4.1)
  • System.Text.Encoding.CodePages (>= 4.4.0)

But all of them are installed automatically.

  • The NuGet package is MySql.Data (8.0.13) (which installs successfully)
  • Project is a .NET Standard 2.0 class library
  • There are no compile errors or even warnings; just the above error at run-time.

Have looked through Could not load file or assembly or one of its dependencies which advises checking where the dependencies are not being found - so I did - but it doesn't say how to fix the missing reference when you've found where it is?

Using Process Monitor I was able to find the failed CreateFile operation DLL calls, referencing ...\TestingGUI\bin\Debug\MySql.Data\MySql.Data.dll which, manually checking, is not there.

The project that runs is a WinForms app that references another .NET Standard class library (essentially a middleman) which then references the database library which depends on MySql.Data.

Doing a search in the whole solution directory, there are no MySql.Data.dll files, especially after a full solution build.

Here is my csproj file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <RootNamespace>App1.Database</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MySql.Data" Version="8.0.13" />
  </ItemGroup>

</Project>

So, who's a .NET Wizard?

UPDATE:

So it turns out the code works fine when run from a .Net Core console app, but only has the error when referenced from a .NET app (specifically winforms). So I've given up having a GUI for now and am just using a .NET Core console app. I thought .NET Standard was compatible with everything, but maybe not? Anyway, I will keep my question here for anyone else having troubles.

UPDATE 2:

Thanks to @Itay Podhajcer's answer we managed to get it working with .NET Winforms by also including the NuGet package there.

Moffen
  • 1,817
  • 1
  • 14
  • 34

2 Answers2

2

I remember encountering the same issue, I think it was related to the new nuget referencing model. Try adding the missing nuget package directly to the WinForms project.Far from ideal solution, but it should work.

Hope it helps!

Itay Podhajcer
  • 2,616
  • 2
  • 9
  • 14
  • Not ideal but you're right, it works! You'd think visual studio would pass its references down, maybe it's a bug. Thankyou!! – Moffen Nov 14 '18 at 20:54
  • 1
    netstandart is the new stuff, but not everything works smoothly with it, net core 3.0 should solve some of the problems, but for that will need to wait and see. – Itay Podhajcer Nov 14 '18 at 20:58
0

Visual Studio not copying dependencies | Could not load file or assembly

It seems the dependencies and assembly are not copied to the output folder. You can check the thread for some more details:

PackageReference is missing copy files to output feature

To resolve this issue, you can try to following workaround:

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135