System.IO.FileNotFoundException with SqlClient

2020-04-02 09:50发布

After fixing my problem as mentioned here I am getting the below exception

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

My library is a .NET Standard 1.4 and the WebApp is .NET Framework 4.6.1

System.Data.SqlClient is version - 4.3.0 NuGet package. So I tried doing the below but in vain:

<dependentAssembly>
      <assemblyIdentity name="System.Data.SqlClient" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
      <bindingRedirect oldVersion="4.1.0.0" newVersion="4.3.0.0"/>
</dependentAssembly>

4条回答
再贱就再见
2楼-- · 2020-04-02 10:05

I guess you may have figured it out already but hope it would save someone precious time

In order to make everything work you would need to reference System.Data.SqlClient in WebApp .NET Framework 4.6.1 project that is referencing your .NET Standard Library. After that everything should work just fine.

Sounds like .NET Standard Library haven't grabbed with itself dependent library binary. There is nothing like "Copy Local" option in .NET Standard references so I don't see any way to check or set this behavior too

查看更多
贪生不怕死
3楼-- · 2020-04-02 10:06

We resolved this by adding System.Data.SqlClient as a reference to our netstandard package, just using the nuget package manager. Once that was done Visual Studio did the rest in the Web solution.

It seems like the dependencies are not all added to the netstandard library when adding a package like Dapper.

查看更多
冷血范
4楼-- · 2020-04-02 10:08

This sent me on a merry chase until I actually compared my project that didn't work, to one that did.

NUGET install for System.Data.SQLClient doesn't necessarily install the files of that name (.dll and .xml) - at least, it didn't for me.

I took those two files from the older project, put them in the bin for the newer, and this solved the problem.

查看更多
▲ chillily
5楼-- · 2020-04-02 10:14

What I tried:

I was also having the same error. I created a sample web api(2.0) which uses a .net standard 2.0 class library. In this library I am using Dapper and Dapper.Contrib and using their extension methods tried to query db. But was having the same error. I tried adding System.Data.SqlClient nuget package in the class library but it did not work. I am using VS2019 Community Edition. This has happened to me twice. So writing the exact scenario and solution that worked for me.

What Worked:

It was eventually resolved by removing System.Data.SqlClient nuget package from the class library and re-adding it in WebApi.

Which is strange since class library is able to resolve dapper nuget package but not System.Data.SqlClient, from within itself.

查看更多
登录 后发表回答