jeudi 25 juin 2020

nable to load one or more of the requested types.\r\nCould not load file or assembly 'System.Data.SqlClient, Version=4.6.1.0

I have an application written on the top of ASP.NET Core 3.1.

When I use reflection to get the types in my project, I get the following exception

Unable to load one or more of the requested types.
Could not load file or assembly 'System.Data.SqlClient, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

I am not sure what package depends on System.Data.SqlClient but my app does not depend on System.Data.SqlClient package.

I tried to install the System.Data.SqlClient into my project the following command, but I am still getting the same error.

Install-Package System.Data.SqlClient -Version 4.6.1

Here is my code

// Loading the dlls.
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();

var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var loadables = referencedPaths.Where(r => !loadedAssemblies.Select(a => a.Location).Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();

foreach(var loadable in loadables)
{
    try
    {
        loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path)));
    }
    catch { }
}


Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
List<Type> types = new List<Type>();

foreach (var assembly in assemblies)
{
    try
    {
        types.AddRange(assembly.GetTypes());
    }
    catch (Exception e)
    {
        // I get the exception here 
        // Unable to load one or more of the requested types.
        // Could not load file or assembly 'System.Data.SqlClient, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
    }
}

How can I determine what is causing this issue? How can I solve it?





Aucun commentaire:

Enregistrer un commentaire