Connection error to Azure SQL Database when running .Net Core console app as Azure webjob


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 ',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": {}
asked on Stack Overflow Mar 13, 2019 by Marwa Ebeid • edited Jun 20, 2020 by Community

1 Answer


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.


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.

answered on Stack Overflow Mar 14, 2019 by Joey Cai

User contributions licensed under CC BY-SA 3.0