Skip to content

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

Open

Description

@chandrashekharjoshi-abb

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

Activity

  1. chandrashekharjoshi-abb commented on Jul 6, 2023

    @chandrashekharjoshi-abb
    Author

    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.

  2. chandrashekharjoshi-abb commented on Jul 6, 2023

    @chandrashekharjoshi-abb
    Author

    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>
    
  3. Kaur-Parminder commented on Jul 7, 2023

    @Kaur-Parminder
    Contributor

    @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.

  4. chandrashekharjoshi-abb commented on Jul 7, 2023

    @chandrashekharjoshi-abb
    Author

    @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.

  5. acardenaskma commented on Aug 8, 2023

    @acardenaskma

    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.

  6. JRahnama commented on Aug 8, 2023

    @JRahnama
    Contributor

    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.

  7. chandrashekharjoshi-abb commented on Aug 9, 2023

    @chandrashekharjoshi-abb
    Author

    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" />
    
  8. jzs11 commented on Aug 24, 2023

    @jzs11

    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. 🤯🤯🤯🤯🤯🤯🤯

  9. JohnyL commented on Jan 4, 2024

    @JohnyL

    Same problem here.

  10. NowinskiK commented on Mar 4, 2024

    @NowinskiK

    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.

  11. jjrobert65 commented on Apr 11, 2024

    @jjrobert65

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

    -Jeff

  12. tomconneely commented on May 28, 2024

    @tomconneely

    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?

  13. Alexbits commented on Jun 27, 2024

    @Alexbits

    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>
    
    
  14. Saibamen commented on Aug 21, 2024

    @Saibamen

    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

  15. iSemihCetin commented on Sep 3, 2024

    @iSemihCetin

    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.

  16. mrihung commented on Dec 19, 2024

    @mrihung

    I had the exact issue and this is how I fix it:

    1. Add these entries to a config file:

    app Settings
    add key="System.Memory" value="bin\System.Memory.dll"
    add key="System.Runtime.CompilerServices.Unsafe" value="bin\System.Runtime.CompilerServices.Unsafe.dll"
    add key="Microsoft.Identity.Client" value="bin\Microsoft.Identity.Client.dll"
    app Settings

    1. Add this function to the Main()

    AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAssembly

    Public Function ResolveAssembly(sender As Object, args As ResolveEventArgs) As Assembly
    Dim baseDirectory = AppDomain.CurrentDomain.BaseDirectory
    Dim appPath As String = Process.GetCurrentProcess().MainModule.FileName
    Dim appName As String = Path.GetFileName(appPath)
    Dim appConfig = New FileInfo(String.Format("{0}{1}{2}", baseDirectory, appName, ".config"))

        Dim configMap = New ExeConfigurationFileMap() With {.ExeConfigFilename = appConfig.FullName}
        Dim config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None)
    
        Dim dllPath As String = Nothing
        If args.Name.Contains("System.Memory") Then
            dllPath = config.AppSettings.Settings("System.Memory")?.Value
        ElseIf args.Name.Contains("System.Runtime.CompilerServices.Unsafe") Then
            dllPath = config.AppSettings.Settings("System.Runtime.CompilerServices.Unsafe")?.Value
        ElseIf args.Name.Contains("Microsoft.Identity.Client") Then
            dllPath = config.AppSettings.Settings("Microsoft.Identity.Client")?.Value
        End If
    
        If Not String.IsNullOrEmpty(dllPath) AndAlso File.Exists(dllPath) Then
            Return Assembly.LoadFrom(dllPath)
        End If
    
        Return Nothing
    End Function
    
  17. edwardneal commented on May 1, 2025

    @edwardneal
    Contributor

    As of the latest versions (6.0.2, 5.2.3 and 5.1.7) of each SqlClient release, this should now be working properly. Can you re-test please?

  18. mrihung commented on May 1, 2025

    @mrihung

    @edwardneal, do you mean I can test it with the latest version of sqlClient (6.0.2)? What are the versions for System.Memory, System.Runtime.CompilerServices.Unsafe and Microsoft.Identity.Client? Thanks

  19. edwardneal commented on May 1, 2025

    @edwardneal
    Contributor

    That's correct @mrihung. When you add a reference to any of those three versions from a .NET Framework 4.x project, it'll pull in transitive references to System.Memory 4.5.5, System.Runtime.CompilerServices.Unsafe 6.0.0, and Microsoft.Identity.Client 4.61.3.

    It should also generate the correct binding redirects in the built application's app.config file (although some projects might switch that off with the "Auto-generate binding redirects" checkbox.)

  20. mrihung commented on May 5, 2025

    @mrihung

    @edwardneal I've tried it with the latest version of SqlClient but still have a similar issue as show below. Do you have any suggestion?

    An error has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

    System.IO.FileLoadException (Inner exception: System.IO.FileLoadException) - Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

  21. moved this from To triage to Needs Response in SqlClient Boardon Jan 12, 2026
  22. added
    Repro Available ✔️
    on Jan 12, 2026
  23. moved this from Needs Response to Backlog in SqlClient Boardon Apr 1, 2026
  24. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  25. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation (SqlClient 5.1.1, .NET target 4.8, SQL Server 2019, Windows Server 2016, repro code included)
    Area Area\Netfx
    Duplicates Potentially related: #801 (closed — similar System.Configuration.ConfigurationManager binding issue on .NET Framework)
    Regression Inconclusive — a maintainer noted this should be resolved as of 6.0.2/5.2.3/5.1.7, but a subsequent reporter re-tested on the latest version and hit an analogous FileLoadException for System.Runtime.CompilerServices.Unsafe instead of System.Memory

    Analysis

    This is a classic .NET Framework binding-redirect problem: when Microsoft.Data.SqlClient is pulled in transitively (here via Microsoft.SqlServer.SqlManagementObjects), the consuming app's app.config doesn't get correct <bindingRedirect> entries for System.Memory/System.Runtime.CompilerServices.Unsafe/Microsoft.Identity.Client, so the runtime fails to load the version SqlClient's assembly actually references. It affects net462 consumers who don't (or can't) auto-generate binding redirects, or whose host app manually loads assemblies via Assembly.LoadFrom. Severity is P2 — impactful for affected consumers but has known workarounds (manual AssemblyResolve handler, adding explicit PackageReferences) and partial fixes have already shipped.

    Next Steps

    • Given the persistence of related FileLoadExceptions across dependencies (System.Memory → System.Runtime.CompilerServices.Unsafe → potentially Microsoft.Identity.Client), recommend assigning to Copilot coding agent to audit the net462 reference assembly dependency versions and nuspec dependency group declarations in Microsoft.Data.SqlClient.csproj/nuspec against .github/instructions/3rd-party-package-versions.instructions.md, confirming binding redirects match the versions actually consumed on net462.
    • Review Could not load type 'System.Configuration.ConfigurationSection' from assembly 'System.Configuration.ConfigurationManager' #801 for prior resolution patterns (adding an explicit direct PackageReference, e.g. to System.Configuration.ConfigurationManager, resolved a similar problem) as a possible workaround to document for affected users.
    • Since the reported symptom has shifted from System.Memory to System.Runtime.CompilerServices.Unsafe across SqlClient versions, verify whether a residual dependency-version mismatch still exists in the latest release before considering this fully resolved.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #2082 · copilot · auto · 49.8 AIC · ⌖ 13.2 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Repro Available ✔️

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions