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

1

6

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.

Aquino answered 21/9, 2021 at 17:6 Comment(0)
J
6

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

Jagir answered 22/9, 2021 at 11:0 Comment(4)
but it works when i make my API project the startup. the problem only exists when i run it as a docker image. i could be wrong but that hints to me that its giving me a weird error but the true problem lies somewhere with docker?Aquino
I’ve hit the same issue when using the publish feature.. somehow the building and publish logic just doesn’t know how to output the right dll. My solutions was to add a hard dependency to the correct file using the copy always setting. Feels like cheating and not very platform agnostic but it works.Heritor
Add the latest version of Microsoft.Data.SqlClient as a NuGet dependency (Current version) and update your all dependencies with latest version. I hope it is not on docker issue. If still issue persist try to redeploy it.Jagir
How can we tell when an application "loads a DLL from a NuGet package directly"? What does this mean, concretely, in Visual Studio, or in code?Blakeney

© 2022 - 2024 — McMap. All rights reserved.