Azure Function not referencing .Net Core Class Library

In what seemed like a simple little application, I started working on an Azure Function that referenced a relatively simple class library that I previously had created. However in the creation of that reference I had build issues and reference issues; at the time I didn’t completely understand the differences between “core”, “framework”, “standard”, “mono”, and so forth; just that I wanted to have this little Azure Function work and not to re-write existing code.

The Error (Compile) Message

I had a simple .Net Core 2.2 Class Library being referenced by an Azure Function v2 and I was receiving an error message at compile

Project [ClassLibraryProject] is not compatibile with netstandard2.0 (.NETStandard,Version=v2.0). Project [ClassLibraryProject] support netcoreapp2.2(.NETCoreapp,Version=v2.2)

.Net Framework Options

How to Use .Net Core in Azure Functions

I started with my normal, default position, that there was something I didn’t know / understand and started asking why an Azure Function (v2) wasn’t able to reference a .Net Core v2 class library; and assumed I just didn’t understand the different framework options available to me (i.e. why in some instances could I consume .net core from .net standard and in others I could not?)

This lead me to Pluralsight and an excellent training module by Barry Luijbregts “The .NET Ecosystem: The Big Picture” which was informative and provided some of the overall background. As well as the deeper dive “What are .Net Framework, .Net Core and Xamarin?” also by Barry Luijbregts. I also found the clear “.Net Standard vs. .Net Core” by David Yardy PE, MCSD.NET that gave the overview of overlaps of the different frameworks.

The “Core” Issue

These are great resources for understanding the differences between the different framework “flavors” but it left me scratching my head. As part of working through those resources I had created a number of different class libraries and console apps and my original question had now morphed into “Why is it, that in some instances I can consume a .Net Core library from a .Net Standard application (example: .Net Standard Console App), but in other instances I could not (example: Azure Function)?”

Finding the Solution

This in turn then provided me with that classic of troubleshooting “find the right search terms to find the right solution” which was more around “Why can I not add a .Net Core Class Library to Azure Function v2” than cut/paste the error message into Google. Which lead me to the StackOverflow (of course) question “Not able to add project reference of .Net Core 2.0 project to Azure function project(netStandard2.0)“. The key being in the comments, but Paul Batum “the current tooling in Visual Studio” which highlights it isn’t so much an issue of the structure, but of the tooling.

Implementing the Solution

Great, I understood the structure of the frameworks and that what I was doing should work, but it wasn’t; so what now?

Brute Force Solution: Two Class Libraries

There was an obvious solution that I had come across in my testing and that was to duplicate the .Net Core Class Library as a .Net Standard Class Library. This of course meant a copy / paste to a new class library and then either maintaining the two libraries or trying to replace the other references with the .Net Standard; this would work, but was a bit of a non-starter.

Clever Solution: Two Target Frameworks

I then came across the clever solution that others have had where the code would be the same but that the target framework would be different, which is that Visual Studio supports, in the csproj file changing the XML tag from “TargetFramework” to “TargetFrameworks” (note the ‘s’) and having the following in my .Net Core Class Library enabled me to utilise the same code across multiple projects both .Net Core projects and Azure Framework projects.

<TargetFrameworks>netcoreapp2.2;net461;netstandard2.0</TargetFrameworks>

Leave a comment