-1

I have .Net Core console app that connects to Azure SQL database using entity framework. Every think works fine locally but when I created publish package using this command line

dotnet publish -c Release -r win-x64 --self-contained false

Zipped it and upload it and used it in Azure webjob I get this exception in the job output:

An error occurred using the connection to database 'myDB' on server 'tcp:myServer.database.windows.net,1433'.

[03/13/2019 19:10:11 > 62ab86: INFO] System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

I added this section to the .json file but it didn't help

"runtimes": {
"win7-x64": {},
"win10-x64": {}
}
Community
  • 1
  • 1
  • 1
    That error message is a definite pointer to "I am a 64-bit process trying to load a 32-bit assembly" (or vice versa). – Brendan Green Mar 13 '19 at 21:36

1 Answers1

1

Apparently, when an .NET Core project is migrated to the csproj format, there are certain conditions under which the resulting csproj will not receive an explicit <PlatformTarget>.

And Visual Studio doesn't seem to behave identically when doing a usual build vs. publishing.

In both bases, it will build an PE32 executable with 32-bits only. But when publishing, it will deploy 64-bit libraries alongside, leading to the aforementioned BadImageFormatException, while it doesn't do that in a non-publish build.

Solution:

In the project>Properties>Build page, select the platform target to x64. Event if it doesn't have any visual effect, an explicit will be added to the csproj on save.

<PlatformTarget>x64</PlatformTarget>
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • This led to different error in the output `"Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'D:\local\Temp\jobs\triggered\FrameworkDependantJob\cryxsmtr.4lv\publish\myProject.dll'. An attempt was made to load a program with an incorrect format.` – Marwa Ebeid Mar 14 '19 at 17:40
  • .NET Core versions 2.2.0 and 2.2.1 are being rolled out as of now and support x64. If you’re running your ASP.NET Core application on .NET Core 2.2 with in-process hosting, you can simply [enable the 64-bit option in the Azure Portal](https://devblogs.microsoft.com/aspnet/asp-net-core-2-2-available-today/) and the site will now run in a 64-bit process. – Joey Cai Mar 15 '19 at 02:27