2

I've created a .NET standard library 2.0 which uses Microsoft.Data.SqlClient Version=1.12.20106.1. I'm referring to this library in the console application (.NET Framework 4.7.2). While making call, I'm getting the error shown below, even though I've added an assembly reference. Can anyone help please?

Could not load file or assembly 'Microsoft.Data.SqlClient, Version=1.12.20106.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' or one of its dependencies. The system cannot find the file specified.":"Microsoft.Data.SqlClient, Version=1.12.20106.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5"}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sachin Chauhan
  • 75
  • 1
  • 1
  • 7

2 Answers2

0

Chances are that in the full error you will see something like

The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

When I experience a similar problem and run a unit test in the debugger then I see this sort of thing in the unit test output Pre-bind state information

Pre-bind state information
LOG: DisplayName = Microsoft.Data.SqlClient, Version=2.0.20168.4, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5
 (Fully-specified)
LOG: Appbase = file:///C:/Dev2/MyApp/UnitTests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : MyApp, Version=1.0.0.2, Culture=neutral, PublicKeyToken=c.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Dev2\myapp\UnitTests.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 2.0.20168.4 redirected to 1.12.20106.1.
LOG: Post-policy reference: Microsoft.Data.SqlClient, Version=1.12.20106.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5
LOG: Attempting download of new URL file:///C:/Dev2/MyApp/UnitTests/bin/Debug/Microsoft.Data.SqlClient.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

The issue is an assembly binding issue the answer here may help. Don't worry that it mentions VB6 the actual issue is independent of that.

Kirsten
  • 15,730
  • 41
  • 179
  • 318
0

This answer is only useful if your netstandard2.0 library has referenced Microsoft.Data.SqlClient as a NuGet package and the referred assembly is not copied over on build.

You may have to use dotnet publish to have the Microsoft.Data.SqlClient assembly copied. It's something that I stumbled upon lately and that I had to fix using a custom resolver. You shouldn't use that resolver but you should check if publishing your application and library helps the issue.

MikeLimaSierra
  • 799
  • 2
  • 11
  • 29