Skip to content

System.BadImageFormatException when targeting .NET Framework #7

Closed

Description

@mgravell

Short version: net46 gives me:

System.BadImageFormatException : Could not load file or assembly 'Microsoft.Data.SqlClient, Version=1.0.19128.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Full repro branch https://github.com/StackExchange/Dapper/commits/ms-sqlclient


I'm using

<PackageReference Include="Microsoft.Data.SqlClient" Version="1.0.19128.1-Preview" />

nuget says this has builds for net46, netstandard2.0 and netcoreapp2.1 so I updated Dapper's tests from

 <TargetFrameworks>net452;netcoreapp1.0;netcoreapp2.0</TargetFrameworks>

to

<TargetFrameworks>net46;netcoreapp2.0;</TargetFrameworks>

(where netcoreapp2.0 should exercise the ns2.0 build)

Now if I use

dotnet test -f net46

I get 287 failures, all of the form:

System.BadImageFormatException : Could not load file or assembly 'Microsoft.Data.SqlClient, Version=1.0.19128.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' or one of its dependencies. An attempt was made to load a program with an incorrect format.

If I use:

dotnet test -f netcoreapp2.0

then it mostly works, but I get 10 failures that I need to investigate and look "real" but different - will log separately.

Activity

  1. mgravell commented on May 10, 2019

    @mgravell
    Author

    (the last 10 were failures to set the UDT type due to hard-coding to SqlParameter - now fixed, although note: it looks like the TDS stream was hella borked after that had failed)

  2. mgravell commented on May 10, 2019

    @mgravell
    Author

    Update: it also fails on net472

  3. mgravell commented on May 10, 2019

    @mgravell
    Author

    Update: I added a global.json to pin it to sdk 2.2.203 (I have v3 preview as default) - still fails on both net46 and net472, so ... yeah, something is very very unhappy on net4*

  4. AfsanehR-zz commented on May 10, 2019

    @AfsanehR-zz

    Thanks for filing this issue. @mgravell from configuration, which platform are you using? is it x86 or x64?

  5. mgravell commented on May 10, 2019

    @mgravell
    Author

    Almost certainly x64, but can check when at PC

  6. mikependon commented on May 10, 2019

    @mikependon

    I have replicated this issue while creating a test project for #5 .

    System.BadImageFormatException: 'Could not load file or assembly 'Microsoft.Data.SqlClient, Version=1.0.19128.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' or one of its dependencies. An attempt was made to load a program with an incorrect format.'

    Target Framework: .Net Framework 4.6.2
    Output Type: Console
    Details: x64
    Assembly: Microsoft.Data.SqlClient, Version=1.0.19128.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5

    I just simply run a very small Console application referencing this Nuget Package.

    --> The issue above is not occurring in NetCore 2.1 Console application.

  7. AfsanehR-zz commented on May 10, 2019

    @AfsanehR-zz

    @mikependon the issue is reproducible if I target AnyCPU. I believe it's coming from the same issue noted in https://github.com/dotnet/SqlClient. Please change your configuration to x64 and see if gets resolved.

  8. mikependon commented on May 10, 2019

    @mikependon

    @afsanehr , sorry for the missed. The issue is not happening at x64 (Net Framework 4.6.2), but is happening both AnyCPU and x86 platform. Thanks

  9. changed the title [-]net46 build is borked?[/-] [+]System.BadImageFormatException when targeting .NET Framework[/+] on May 10, 2019
  10. David-Engel commented on May 10, 2019

    @David-Engel
    Contributor

    This is to be expected until we split the native code out (it's currently a single mixed mode DLL) and change the managed code to load the appropriate native DLL at runtime. We are working on that now and the change is targeted for the next preview.

  11. added this to the 1.0.0 milestone on May 20, 2019
  12. cheenamalhotra commented on Jul 31, 2019

    @cheenamalhotra
    Member

    Hi @mgravell

    The latest version of Microsoft.Data.SqlClient splits SNI in a separate Nuget package Microsoft.Data.SqlClient.SNI

    Could you give it a try and let us know if issue persists?

    Thanks!

  13. self-assigned this
    on Aug 1, 2019
  14. cheenamalhotra commented on Aug 20, 2019

    @cheenamalhotra
    Member

    Closing as SNI split fixes the issue.

  15. added a commit that references this issue on Apr 28, 2026
    458777a
  16. added a commit that references this issue on May 29, 2026
    d8eb64e
  17. added 3 commits that reference this issue on Jun 15, 2026
    68923b1
    7c83a3c
    344c87a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions