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": {}

1 answer

  • answered 2019-03-14 07:57 Joey Cai

    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.