4

I am creating an Azure Function in C# using a target framework of netstandard2.0 in a Windows 10 environment. The function calls a method that's in another class library and that method creates an instance of SqlConnection. When I run the function I get the following exception:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.RefreshImages ---> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ---> System.PlatformNotSupportedException : System.Data.SqlClient is not supported on this platform. at System.Data.SqlClient.SqlConnection..ctor(String connectionString)......

Obviously SqlConnection is supported on Windows so I assume there's something else going on here.

Paul Hunt
  • 3,395
  • 2
  • 24
  • 39
  • 1
    I had a similar issue while referring a .netstandard library inside a windows service (which is in .net framework). I had to add the System.Data.SqlClient nuget reference to the windows service project also to make it work. Turning the .netstandard library into a nuget and then referring it also seems to work and seems a better approach. I am right now looking into that. – ronilk Apr 18 '18 at 11:27

2 Answers2

3

This happens when a .Net Standard lib that uses a SqlConnection is loaded dynamically via reflection. The .Net Standard lib will typically reference System.Data.SqlClient which seems to be a dummy lib without actual implementation. It apparently ensures that the lib will compile on all platforms, including those without Registry and other platform specific stuff that the real SqlClient implementation relies on.

The easiest solution I can find is to add a reference to the Microsoft.Data.SqlClient NuGet package in the host application (the .Net core application that dynamically loads the .Net standard lib).

You may see a small yellow warning exclamation icon in the Solution explorer because Visual Studio thinks you are not using the lib and if you use the "Remove Unused References" feature it will also suggest removing the package. There is a feature to suppress warnings in the PropertyGrid, but I cannot figure out wat number should be filled in since the warning does not appear in the error list when compiling...

Louis Somers
  • 2,560
  • 3
  • 27
  • 57
  • 1
    I ran into this. I also had to install the System.CodeDom NuGet package. However, I am 100% .NET 6, without any .net standard, so the assumption that this is a problem because of .net standard may not be accurate. – Rhyous May 04 '22 at 00:35
1

It looks like this is related to loading a SQL connection via reflection in .NET core (you are running on netstandard2.0 but the principle should still be the same).

Connor McMahon
  • 1,318
  • 7
  • 15
  • Any pointers as to what kind of changes I need to make? The library is netstandard2.0 too and is being called from an ASP.NET Core web site without an issue. – Paul Hunt Dec 08 '17 at 18:43
  • Hmm, if the library is not having issues on a .NET Core web site, that is indeed confusing. If this is only a problem on Azure Functions, then it could be a problem with how we are managing the dependencies, as that is what is implied in the GitHub issue above. I will remove the section of my answer implying something is wrong with the library. Does this work if you make your Function with a target framework other than `netstandard2.0`? – Connor McMahon Dec 08 '17 at 19:23