Forum Discussion

alex-1337's avatar
alex-1337
Copper Contributor
Oct 10, 2022

Could not load file or assembly System.Data.SqlClient

Hello,

I'm currently learning the ASP.NET Core-Web-API (.NET 6) system. I added a class-library (.NET Framework) (v4.8) as a DataAccessLayer to my project. There is a DAO class with a sql-query that uses Dapper and System.Data.SqlClient.

just for example:

 

var parameters = new { id };
using (SqlConnection connection = new SqlConnection("...")) {
  connection.Open();
  return connection.ExecuteScalar<bool>(query, parameters);
}

 

 

ASP.NET Core-Web-API

- Controller

Lib

- DAO-Class

 

Now my Problem: When I try to invoke the method which contains this query:

 

[HttpGet("foo")]
public ActionResult<bool> Foo(DTO request) {
  var isOk = Lib.Dao.FetchSomething(request);
  return Ok(isOk);
}

 

, I'll get the following error on line 3:

 

System.IO.FileNotFoundException: "Could not load file or assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified."

 

My lib has the reference to System.Data (4.0.0.0) and System.Data.SqlClient (4.6.1.3).

Why do .net tries to search the version 0.0.0.0?

Why does this doesn't work? This should work (worked for me on .NET WebForms)

 

Regards.

5 Replies

  • govindjaiswal's avatar
    govindjaiswal
    Copper Contributor

     hii alex-1337  

    The error message you're encountering indicates that the system is unable to find the specified version of the 'System.Data.SqlClient' assembly. The version number mentioned in the error message, 'Version=0.0.0.0', typically appears when the system is unable to determine the correct version of the assembly.

    In .NET, when a referenced assembly is not explicitly specified with a version number, the runtime attempts to load the assembly with the version defined in the application's configuration file or with the latest version available in the Global Assembly Cache (GAC). However, if it fails to locate the assembly with the specified version, it falls back to the default version number '0.0.0.0'.

    To resolve this issue, you can try the following steps:

    1. Make sure that the 'System.Data.SqlClient' assembly is present in your project's output directory or in a location where it can be discovered by the runtime. If the assembly is missing, you might need to add it as a reference to your project.

    2. Check if you have any configuration files in your project (e.g., 'app.config' or 'web.config') that specify a specific version of the 'System.Data.SqlClient' assembly. Ensure that the version specified in the configuration file matches the version of the assembly you have referenced.

    3. Verify that the 'System.Data.SqlClient' assembly version you referenced (4.6.1.3) is compatible with .NET Core and the .NET 6 version you're using. While the assembly might have worked in .NET WebForms, there could be compatibility issues when using it in .NET Core. Consider using a compatible alternative or updating the assembly to a version specifically designed for .NET Core.

    4. If you're using a NuGet package to reference 'System.Data.SqlClient', ensure that you have the correct version installed. Try updating the package to the latest version compatible with .NET 6.

     

    By following these steps, you should be able to resolve this. 

  • alex-1337's avatar
    alex-1337
    Copper Contributor

    I found a solution, without knowing why this is a solution, for this problem.

     

    I have two projects:

    1. ASP.NET Core-Web-API (.NET 6)

    2. Class-Library (.NET Framework) (Framework v4.8)

     

    The connection to the database happens with Dapper and System.Data.SqlClient. Both are added to the references of the the Class-Library, but not added to the dependencies of the the Web-API. This should work, because the Class-Library should provide the SqlClient and Dapper, but it doesn't. It provides only Dapper to the Web-API. Idk why, but I needed to add the System.Data.SqlClient with NuGet to my Web-API dependencies too. I'll never use the SqlClient in my Web-API in furure. Currently it's a bug in the ASP.NET Core-Web-API in my opinion. I would be very grateful, if someone could explain this behaviour to me.

     

    Regards

    • mBardos76's avatar
      mBardos76
      Copper Contributor

      alex-1337 anyone stumbles on this, the System.Data.SqlClient.dll will be copied with the class library, if you add the following lines to your class library's app.config:
      <entityFramework>
        ...
         <providers>
             <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
          </providers>
      </entityFramework>

  • LanHuang's avatar
    LanHuang
    Iron Contributor

    Hi alex-1337,

    Thanks for posting your issue here.

    However this platform is used for how-to discussions and sharing best practices for building any app with .NET.Since your issue is a technical question, welcome to post it in Microsoft Q&A forum, the support team and communities on Microsoft Q&A will help you for any technical questions.
    Besides, it will be appreciated if you can share it here once you post this technical question Microsoft Q&A.
    Best Regards,
    Lan Huang

    • alex-1337's avatar
      alex-1337
      Copper Contributor

      Hello LanHuang

      thank you for this information. I'll ask my question there.

       

      Regards

       

Resources