Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 5.0 published winforms application return System.BadImageFormatException on db access #2617

Closed
worldofsites opened this issue Nov 13, 2020 · 13 comments

Comments

@worldofsites
Copy link

After updating winforms core 3.1 app with linq2db reference to net5.0 (need the ability to deploy through clickonce) and publishing to folder (without clickonce) and after running that app I see System.BadImageFormatException on any db access.
All db queries works when on debug.
db: MSSQL
linq2db version: 3.1.6
publish target framework: net5.0-windows
target runtime: win-x64 and win-x86 - reproduced for both
stacktrace:

System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)
  at System.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
  at System.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token)
  at System.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrentNative()
  at System.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent()
  at System.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory)
  at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
  at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
  at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
  at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
  at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
  at System.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken)
-- End of stack trace from previous location ---
  at LinqToDB.Data.DataConnection.EnsureConnectionAsync(CancellationToken cancellationToken)

Also, when I've published that app via clickonce, installing it and trying to start I see the following entry in the Windows Application Event Log after crash:

An assembly specified in the application dependencies manifest (TestApp.deps.json) was not found:
    package: 'runtime.win-x64.runtime.native.System.Data.SqlClient.sni', version: '4.4.0'
    path: 'runtimes/win-x64/native/sni.dll'
@viceroypenguin
Copy link
Contributor

Question: Have you made a direct dependency on System.Data.SqlClient? If not, what happens if you do?

@worldofsites
Copy link
Author

Yes, I've made direct dependency on System.Data.SqlClient, because without System.Data.SqlClient I see errors even on debug:

System.InvalidOperationException: Cannot load assembly System.Data.SqlClient
  at LinqToDB.DataProvider.SqlServer.SqlServerProviderAdapter.CreateAdapter(String assemblyName, String clientNamespace, String factoryName)
  at LinqToDB.DataProvider.SqlServer.SqlServerProviderAdapter.GetInstance(SqlServerProvider provider)
  at LinqToDB.DataProvider.SqlServer.SqlServerDataProvider..ctor(String name, SqlServerVersion version, SqlServerProvider provider)
  at LinqToDB.DataProvider.SqlServer.SqlServerTools.<>c.<.cctor>b__33_2()
  at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
  at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
  at System.Lazy`1.CreateValue()
  at System.Lazy`1.get_Value()
  at LinqToDB.DataProvider.SqlServer.SqlServerTools.GetDataProvider(SqlServerVersion version, SqlServerProvider provider)
  at LinqToDB.DataProvider.SqlServer.SqlServerTools.ProviderDetector(IConnectionStringSettings css, String connectionString)
  at LinqToDB.Data.DataConnection.ConfigurationInfo.<>c__DisplayClass12_0.<GetDataProvider>b__0(Func`3 d)
  at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
  at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
  at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
  at LinqToDB.Data.DataConnection.ConfigurationInfo.GetDataProvider(IConnectionStringSettings css, String connectionString)
  at LinqToDB.Data.DataConnection.ConfigurationInfo.get_DataProvider()
  at LinqToDB.Data.DataConnection..ctor(LinqToDbConnectionOptions options)
  at LinqToDB.Data.DataConnection..ctor(LinqToDbConnectionOptionsBuilder builder)
  at LinqToDB.Data.DataConnection..ctor(String configurationString)

@viceroypenguin
Copy link
Contributor

Which version of System.Data.SqlClient? I see 4.4.0 in the event log note above. Are you using 4.8.2 nuget package? (I know that the version numbers don't always match between the dll/sni version and the nuget version, and I haven't looked, so I'm asking.)

@worldofsites
Copy link
Author

System.Data.SqlClient 4.8.2 version

@viceroypenguin
Copy link
Contributor

I just created a quick dummy app to verify. After publishing, it worked fine for me. If you want, I can upload to GH so you can see if it works for you.

One thing you might check, under the publish directory, do you see runtimes\win-x64\native\sni.dll? Based on both errors above (not being able to find the file after CO publish; and BadImageException on running after publish), I think this file may not be getting into the publish directory properly.

@worldofsites
Copy link
Author

I just created a quick dummy app to verify. After publishing, it worked fine for me. If you want, I can upload to GH so you can see if it works for you.

Yes, it would be helpful, thanks!

do you see runtimes\win-x64\native\sni.dll?

No, it's not in my publish directory

@viceroypenguin
Copy link
Contributor

Download this repo here: https://github.com/viceroypenguin/l2db-publish-net50

If that doesn't work, then there's a problem with the build/publish system on your machine; I'd suggest a repair of Visual Studio/reinstall of .net5.0

If that does work, then there's a dependency management problem. Try restoring packages? Or open the package manager console and execute Update-Package -Reinstall. The first should work, the second forces VS to refresh the dependency chain. Hopefully that will help it to recognize the issues.

@worldofsites
Copy link
Author

Thanks!
Checked your Dummy app:

  1. BadImageException on running after publish - is not reproduced. Still investigating whats wrong in my app.
  2. not being able to find the file after CO publish - reproduced. If run Dummy.exe from publish directory, then okay, but if I install app with Dummy.application and try to run it as windows app, then app not opened and in Windows Application Event Log:
\Dummy.exe
Message: Error:
  An assembly specified in the application dependencies manifest (Dummy.deps.json) was not found:
    package: 'runtime.win-x64.runtime.native.System.Data.SqlClient.sni', version: '4.4.0'
    path: 'runtimes/win-x64/native/sni.dll'

What in my CO publish directory:
image

@viceroypenguin
Copy link
Contributor

That sounds like a bug either in CO or in System.Data.SqlClient. I'd report upstream to dotnet/runtime and see if they have any ideas.

You might try switching to Microsoft.Data.SqlClient. It is the newer path, and they'll likely suggest doing the same. To bind w/ l2db, you can use

SqlServerTools.GetDataProvider(
        SqlServerVersion.v2017, // or whichever version of server you're currently using
        SqlServerProvider.MicrosoftDataSqlClient

to get a DataProvider, and pass the connection string as the second argument. Should require no other changes to l2db.

@worldofsites
Copy link
Author

@viceroypenguin thanks for your help, I'll check Microsoft.Data.SqlClient.
I have already created issue in dotnet/runtime for similar case with CO publish, but with Microsoft.PowerShell libraries -> dotnet/runtime#44602

@viceroypenguin
Copy link
Contributor

Glad to help how I can! I'm sure we'll see plenty of these friction bugs over the next couple months. Hopefully they won't take too long to wash out.

@worldofsites
Copy link
Author

Same for Microsoft.Data.SqlClient:

Message: Error:
  An assembly specified in the application dependencies manifest (Dummy.deps.json) was not found:
    package: 'Microsoft.Data.SqlClient.SNI.runtime', version: '2.0.1'
    path: 'runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.dll'

@viceroypenguin
Copy link
Contributor

Darn. Sorry! Hope MSFT resolves it for ya quickly.

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

No branches or pull requests

2 participants