dotnet / SqlClient

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

System.BadImageFormatException when targeting .NET Framework #7

Closed mgravell closed 5 years ago

mgravell commented 5 years ago

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.

mgravell commented 5 years ago

(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)

mgravell commented 5 years ago

Update: it also fails on net472

mgravell commented 5 years ago

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*

AfsanehR-zz commented 5 years ago

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

mgravell commented 5 years ago

Almost certainly x64, but can check when at PC

mikependon commented 5 years ago

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.

AfsanehR-zz commented 5 years ago

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

mikependon commented 5 years ago

@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

David-Engel commented 5 years ago

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.

cheenamalhotra commented 5 years ago

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!

cheenamalhotra commented 5 years ago

Closing as SNI split fixes the issue.