Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0

Tags:

c#

First of, some context information:

The platform this is running on has .Net Framework 4.7.1 installed. I have a Class Library that is in the .Net Standard 2 specification in order to support .Net Core in the future. Now parts of dependencies, namely Dapper, uses System.Data.SqlClient. This library works just fine on my own machine but I run into problems when I deploy and test it on my Windows 2012 server. Namely, I have a runtime error when Dapper is used: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=kfddsnfsjnfs' or one of its dependencies. The system cannot find the file specified.

Mind you I first had version 4.5.1.0 installed. I then downgraded to 4.4.0.0 and rerrun the code. Now I got the same error but this time regarding 4.2.0.0. But I cannot seem to find this particular version on Nuget. After this I googled. A lot. First I tried adding rebindining the old version with a new by adding both a

appsettings.json:

{
  "dependentAssembly": {
    "assemblyIdentity": {
      "name": "System.Data.SqlClient",
      "publicKeyToken": "kfddsnfsjnfs",
      "culture": "neutral"
    },
    "bindingRedirect": {
      "oldVersion ": "4.4.0.0",
      "newVersion": "4.5.1"
    }
  }
}

and app.config:

<dependentAssembly>
  <assemblyIdentity name="System.Data.SqlClient"  publicKeyToken="kfddsnfsjnfs"  culture="neutral" />
  <bindingRedirect oldVersion="4.4.0.0" newVersion="4.5.1.0" />
</dependentAssembly>

However it didn't make a difference. I have also tried older versions of the SqlClient and multiple reinstalls. I also found people who said to double check the csproj file so it didn't reference something in the gac, but it does not:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Authors>me</Authors>
    <Product />
    <Company />
    <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
    <Version>1.0.8</Version>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="TaskMetadata.json">
      <PackagePath>TaskMetadata.json</PackagePath>
      <Pack>True</Pack>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="dapper" Version="1.50.5" />
    <PackageReference Include="itextsharp" Version="5.5.13" />
    <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="External\" />
  </ItemGroup>

</Project>
like image 817
Krille Avatar asked Nov 05 '18 17:11

Krille


2 Answers

Each library runs under the process of the main application. So the main application needs to know to load the SqlClient DLL. So the parent project (the .NET 4.7.1 project) needs to have SqlClient added as a reference, either by installing the NuGet package, or just adding a reference by browsing to the DLL under the .NET Standard project.

Old answer: That version exists in NuGet: https://www.nuget.org/packages/System.Data.SqlClient/4.4.0

In the Package Manager Console (make sure the 'Default project' drop-down is set to the right one), try uninstalling and then reinstalling that specific version:

Uninstall-Package System.Data.SqlClient
Install-Package System.Data.SqlClient -Version 4.4.0

Update: Or, in your binding redirect, just use 4.2.0.0 as the oldVersion.

like image 120
Gabriel Luci Avatar answered Oct 13 '22 15:10

Gabriel Luci


install version 4.8.2 in both project

Install-Package System.Data.SqlClient -Version 4.8.2

like image 40
hassan kamarzadeh Avatar answered Oct 13 '22 13:10

hassan kamarzadeh