1

We are upgrading our solution of multiple projects. Starting with one class library which is been referenced by 13 projects. I need to upgrade my class library project .NET Framework 4.5 to .NET Standard 2.1. Here the problem is:

This class library is being referenced by projects of .NET Framework 4.5 and its throwing error that it can't access .NET Standard 2.1 lib

Project 'abc.csproj' targets 'netstandard2.1'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.5'.

How to resolve this problem? Upgrading all those 13 projects is not possible at the same time.


EDIT: Following comments, if I convert lib to .netstandard1.1 then errors start coming in the library itself: enter image description here

After downgrading lib to .netstandard1.1, following on NetStandard 1.4 does not allow decorating class with [DataContract]. I saw this post and added this package System.Runtime.Serialization.Primitives: enter image description here

I even added reference to older dll but, still getting error: enter image description here

phuzi
  • 12,078
  • 3
  • 26
  • 50
Annie
  • 670
  • 1
  • 7
  • 20
  • 1
    Well, per [this article](https://learn.microsoft.com/en-us/dotnet/standard/net-standard) it seems that `.NET Standard 2.1` is not following along with `.NET Framework`. Latest you can get is `.NET Standard 2.0` with `.NET Framework 4.6.1`. – Tatranskymedved Nov 25 '21 at 09:37
  • 1
    So if all applications are at `.NET Framework 4.5`, you should upgrade your class library to version `.NET Standard 1.1`. This should work, once you upgrade also all other applications to the standard, you might upgrade everything again to the higher veresion of .NET Standard. – Tatranskymedved Nov 25 '21 at 09:42
  • 2
    Changing it to `.NET Standard 1.1` also brings some issues. As you are mentioning another error, answer for this can be found in NuGet package, that must be downloaded. See https://stackoverflow.com/questions/43950931/netstandard-1-4-does-not-allow-decorating-class-with-datacontract for more details. – Tatranskymedved Nov 25 '21 at 10:16
  • I've just created 2 projects (one with `.NET Framework 4.5`, other `.NET Standard 1.1`). In library I have `[DataMember]` on some properties, VS asked me to download Nuget pckg: `System.Runtime.Serialization.Primitives` (version 4.3.0). After doing that, I was normally able to build it. Please try to run **build solution and provide with the output**. – Tatranskymedved Nov 25 '21 at 10:37
  • Now the problem is that your "end application" and "library" is both using those serializations -> but they are using different version of DLL. One is coming from Nuget, other from "system assembly". What you need to do is remove all references to system assembly (`System.Runtime.Serialization`) and use Nugets everywhere instead. – Tatranskymedved Nov 25 '21 at 10:44

1 Answers1

2

Adding answer as we've iterated through the comments. At first OP wanted to change framework of library to .NET Standard 2.1 while having other apps under .NET Framework 4.5.

Unfortunately, this is not possible. As specified in the official documentation, .NET Standard 2.1 is not following along with .NET Framework. Latest you can get is .NET Standard 2.0 with .NET Framework 4.6.1. Solution for this issue is to downgrade common library project to .NET Standard 1.1, other app projects should be able to link to this.

Next iteration (after downgrading to .NET Standard 1.1) brought issue related to "unknown classes" and VS/compiler asking for adding reference. There was already existing question, answer is to download NuGet package related to the data serializers.

If your code in end-applications are also using classes/attributes from the affected assembly, you must make a cleanup. So that all code references the only single version of source. It is not possible to have one Attribute taken from Nuget and check for "same attribute/same name" but from different source (system assembly -> System.Runtime.Serialization).

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • 2
    Or, you know, upgrade the .NET Framework projects to 4.8. Framework 4.5 is more than four years old and [support will end as of April 2022](https://learn.microsoft.com/en-us/lifecycle/faq/dotnet-framework). Upgrading to 4.8 will give you security updates and give access to .NET Standard 2.0. – CodeCaster Nov 25 '21 at 10:45
  • 1
    Well, OP mentions that upgrading of all projects altogether is not possible. That's why I'm going this way. :/ – Tatranskymedved Nov 25 '21 at 10:50