Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.PlatformNotSupportedException when referencing System.Data.SqlClient ONLY when running as a docker image

I'm trying to run my azure function application as a docker image and when I do it throws me a System.PlatformNotSupportedException in Microsoft.Data.SqlClient.dll: 'Strings.PlatformNotSupported_DataSqlClient' exception any time I try to call my datalayer (no matter if its EF Core or Dapper that I use). I can run the application just fine when setting my API project as startup instead of the docker image.

There are very similar questions out there but none of them take into account that it is ONLY happening when I try to run it as a docker image or the fact that my target framework is netcoreapp3.1. I've already tried what the similar questions said. which is to add System.Data.SqlClient as a Nuget package which doesn't help no matter what version I make it.

I suspect I need to add something to the Dockerfile or something.

here is some additional information: enter image description here

EDIT: I am able to recreate the issue in a new project by simply creating an azure function, adding docker support, creating a simple data layer that performs a CRUD operation on a local database, then running the docker image and calling the endpoint that does the CRUD operation. Hope that helps.

like image 287
MistaGoustan Avatar asked Oct 15 '22 19:10

MistaGoustan


1 Answers

The Microsoft.Data.SqlClient NuGet package includes a number of DLLs supporting different .NET targets and different runtime platforms. If you are getting a

PlatformNotSupported Exception

it ultimately means your application is not loading the appropriate DLL.

A different DLL for .NET Framework, .NET Core, .NET Standard, Windows, Linux, etc. The NuGet infrastructure will automatically reference and load the appropriate DLL based on your application's needs.

If your application loads a DLL from a NuGet package directly, it bypasses all this logic and probably loads the incorrect DLL.

The DLL in the NuGet package under lib/netstandard2.0/Microsoft.Data.SqlClient.dll is basically the fallback DLL for any unsupported target and simply throws the PlatformNotSupported exception for any call. This is a nicer exception than what you would otherwise get when running on a platform that does not have a DLL built for it. Ultimately, you want to use the NuGet package reference infrastructure or you would have to implement all this target framework and platform support logic yourself when determining which DLL to load.

Additionally, the NuGet package contains all the dependency information for the SqlClient library and facilitates the downloading and referencing of dependencies. If you reference and load an individual DLL manually, it is up to you to ensure all dependencies are also available to the SqlClient library.

Refer why do i get a platformnotsupported exception when my application hits a sqlclient method

You can update all your dependencies with latest versions. It may fix your problem. Please check the implemented target frameworks and platforms support the logic of your code.

Refer here Link 1 & Link 2

like image 145
Delliganesh Sevanesan Avatar answered Oct 20 '22 18:10

Delliganesh Sevanesan