dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
851 stars 286 forks source link

Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. #2082

Open chandrashekharjoshi-abb opened 1 year ago

chandrashekharjoshi-abb commented 1 year ago

Describe the bug

When i add nuget package Microsoft.SqlServer.SqlManagementObjects 170.18.0 for SMO APIs(it also adds lot of dependecy packages), One of the dependency is Microsoft.Data.SqlClient, which is using old version of System.Memory 4.0.1.1 while package downloads 4.0.1.2. image

we always get exception on SqlConnection.Open() function call. below is the fusion log:

Pre-bind state information LOG: DisplayName = System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 (Fully-specified) LOG: Appbase = file:///C:/ABB.SDS.SDK/host/Server/ LOG: Initial PrivatePath = NULL Calling assembly : Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5.

LOG: This bind starts in LoadFrom load context. WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). LOG: Using application configuration file: C:\ABB.SDS.SDK\host\Server\MOMServices.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/System.Memory.DLL. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/System.Memory/System.Memory.DLL. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/Apps/Configurator/desktop/System.Memory.DLL. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/Apps/Configurator/desktop/System.Memory/System.Memory.DLL. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/System.Memory.EXE. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/System.Memory/System.Memory.EXE. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/Apps/Configurator/desktop/System.Memory.EXE. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/Apps/Configurator/desktop/System.Memory/System.Memory.EXE. LOG: Attempting download of new URL file:///C:/ABB.SDS.SDK/host/Server/Apps/ABB.SDS.Batch.Accessors.Server/server/System.Memory.DLL. WRN: Comparing the assembly name resulted in the mismatch: Revision Number

To reproduce

Use Microsoft.SqlServer.SqlManagementObjects 170.18.0 and exectute SqlConnection

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

using (SqlConnection con = new SqlConnection())
            {
               con.ConnectionString = Utility.GetConnectionString(connectionParameter);
               if (connectionParameter.UseAuthenticationMode == AuthenticationMode.Sql)
               {
                  SqlCredential cred = new SqlCredential(connectionParameter.UserName, connectionParameter.Password);
                  con.Credential = cred;
               }

               Server server = new Server(new ServerConnection(con));
               // Open connection.
               con.Open();
               if (con.State != System.Data.ConnectionState.Open)
                  return false;

               if (server.VersionMajor < connectionParameter.ServerVersionNumber)
               {
                  throw new NotSupportedException("This SQL server version is not supported. The minimum version number is " + connectionParameter.ServerVersionNumber.ToString(CultureInfo.CurrentCulture));
               }

               return true;
            }

Expected behavior

A clear and concise description of what you expected to happen.

Further technical details

Microsoft.Data.SqlClient version: Nuget version 5.1.1 .NET target: 4.8 SQL Server version: 2019 Operating system: Windows Server 2016

chandrashekharjoshi-abb commented 1 year ago

Update: if i change System.Memory nuget version to 4.5.4 which is 4.0.1.1, the i get error for System.Runtime.CompilerServices.Unsafe.

Pre-bind state information === LOG: DisplayName = System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a (Fully-specified) LOG: Appbase = file:///C:/ABB.SDS.SDK/host/Server/ LOG: Initial PrivatePath = NULL Calling assembly : System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51.

LOG: This bind starts in LoadFrom load context. WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). LOG: Using application configuration file: C:\ABB.SDS.SDK\host\Server\MOMServices.exe.Config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a LOG: The same bind was seen before, and was failed with hr = 0x80070002.

chandrashekharjoshi-abb commented 1 year ago

Finally i could make it work by changing the version manually in packages.config file as below:

<packages>
  <package id="Azure.Core" version="1.25.0" targetFramework="net48" />
  <package id="Azure.Identity" version="1.7.0" targetFramework="net48" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.1" targetFramework="net48" />
  <package id="Microsoft.Data.SqlClient" version="5.1.1" targetFramework="net48" />
  <package id="Microsoft.Data.SqlClient.SNI" version="5.1.0" targetFramework="net48" />
  <package id="Microsoft.Identity.Client" version="4.47.2" targetFramework="net48" />
  <package id="Microsoft.Identity.Client.Extensions.Msal" version="2.19.3" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Abstractions" version="6.24.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.JsonWebTokens" version="6.24.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Logging" version="6.24.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Protocols" version="6.24.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="6.24.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Tokens" version="6.24.0" targetFramework="net48" />
  <package id="Microsoft.SqlServer.Assessment" version="1.1.9" targetFramework="net48" />
  <package id="Microsoft.SqlServer.Assessment.Authoring" version="1.1.0" targetFramework="net48" />
  <package id="Microsoft.SqlServer.SqlManagementObjects" version="170.18.0" targetFramework="net48" />
  <package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
  <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
  <package id="System.Configuration.ConfigurationManager" version="6.0.1" targetFramework="net48" />
  <package id="System.Diagnostics.DiagnosticSource" version="6.0.0" targetFramework="net48" />
  <package id="System.IdentityModel.Tokens.Jwt" version="6.24.0" targetFramework="net48" />
  <package id="System.IO" version="4.3.0" targetFramework="net48" />
  **<package id="System.Memory" version="4.5.4" targetFramework="net48" />**
  <package id="System.Memory.Data" version="1.0.2" targetFramework="net48" />
  <package id="System.Net.Http" version="4.3.4" targetFramework="net48" />
  <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
  <package id="System.Runtime" version="4.3.0" targetFramework="net48" />
  **<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />**
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.AccessControl" version="6.0.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.ProtectedData" version="4.7.0" targetFramework="net48" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net48" />
  <package id="System.Security.Permissions" version="6.0.0" targetFramework="net48" />
  <package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
  <package id="System.Text.Encoding" version="4.3.0" targetFramework="net48" />
  <package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net48" />
  <package id="System.Text.Json" version="4.7.2" targetFramework="net48" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>
Kaur-Parminder commented 1 year ago

@chandrashekharjoshi-abb I am looking into the issue. I will update here, once I have it reproduced. As a workaround you can give try to assembly-redirect.

chandrashekharjoshi-abb commented 1 year ago

@chandrashekharjoshi-abb I am looking into the issue. I will update here, once I have it reproduced. As a workaround you can give try to assembly-redirect.

Thank you for the response, i have tried assembly redirect and it didn't fix the issue, but i can give it another try by referring to the link.

acardenaskma commented 1 year ago

I am facing the same issue with Microsoft.Data.SqlClient v5.02. Assembly redirects won't work because I'm using an Excel COM add-in and don't have access to the Excel app config file. The only solution I have found so far is to add the redirect to the machine.config, but that's not practical for clients' computers.

JRahnama commented 1 year ago

I faced similar issue while working on WinForms. I added the nuget from Nuget.org through visual studio nuget package manager and inside the app.config I set the value to be like below somewhere inside configuration tag:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe"
                          publicKeyToken="b03f5f7f11d50a3a"
                          culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.6.0"
                         newVersion="4.0.6.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Depending on what version you installed you can redirect to the correct number. I did not check if adding the package could fix the issue without changing the app.config.

chandrashekharjoshi-abb commented 1 year ago

Assembly redirect did not work for, since it created chain of errors, after solving one another appears. for me final solution was to do below changes in package.config, changed to lower versions for below package.

<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
jzs11 commented 1 year ago

I am facing the same problem, and it ends up updating almost 50+ configs and add binding redirect everywhere for an enterprise software I am working on. 🤯🤯🤯🤯🤯🤯🤯

JohnyL commented 10 months ago

Same problem here.

NowinskiK commented 8 months ago

I have the same problem when trying to create SqlConnection object in PowerShell, see cold below:

$cns = "Data Source=$SqlServerName;Initial Catalog=$SQLDbName;Authentication=Active Directory Default;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
$sqlcc = new-object ('Microsoft.Data.SqlClient.SqlConnection') $cns

Error: image

The above issue also happens when you use the SqlServer module as it uses DaxFx underneath.

jjrobert65 commented 7 months ago

Same. Mine is a .NET Framework Web API generated by the latest VS 2022.

-Jeff

tomconneely commented 5 months ago

I'm working on a Windows Forms application and I am receiving the error stating that "Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"

Is there a fix for this issue?

Alexbits commented 4 months ago

This helped me in my case

 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
    <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Saibamen commented 2 months ago

The same for me when I was trying to update .NET Framework lib to use latest EF6 6.5.1 together with Microsoft.Data.SqlClient version 5.2.1 and trying to use new Microsoft.Data.SqlClient provider

Command: Add-Migration UpdateEF6 -ConnectionStringName Dev

EDIT @Alexbits code also worked for me. Thanks

resmigenc commented 2 months ago

Hi, thanks for the suggestion, the solution worked.

Benim de başıma aynısı geldi, .NET Framework kütüphanesini en son EF6 sürümüyle 6.5.1birlikte güncellemeye ve yeni sağlayıcıyı kullanmaya çalıştığımda.Microsoft.Data.SqlClient``5.2.1``Microsoft.Data.SqlClient

Emretmek: Add-Migration UpdateEF6 -ConnectionStringName Dev

DÜZENLEMEK@Alexbitskod benim için de işe yaradı. Teşekkürler

The same for me when I was trying to update .NET Framework lib to use latest EF6 6.5.1 together with Microsoft.Data.SqlClient version 5.2.1 and trying to use new Microsoft.Data.SqlClient provider

Command: Add-Migration UpdateEF6 -ConnectionStringName Dev

EDIT @Alexbits code also worked for me. Thanks

Hi, thanks for the suggestion, the solution worked.