14

I'm trying to get .Net Framework and NetStandard assemblies to communicate with each other (to learn what is possible). I currently have four projects, two Framework 4.5.2 projects and two NetStandard1.2 projects:

  1. Framework452.Library
  2. NetStandard12.CentralLibrary
  3. NetStandard12.BaseLibrary
  4. Framework452.Tests

The referencing structure is:

  • Framework452.Tests references NetStandard12.CentralLibrary: working by adding the NetStandard.Library nuget package to Framework452.Tests.
  • NetStandard12.CentralLibrary references NetStandard12.BaseLibrary: working without modification.
  • NetStandard12.CentralLibrary references Framework452.Library: Not working, even when Framework452.Library has the NetStandard.Library nuget package installed.

Can NetStandard projects reference Framework projects? If so, what do I need to do to get them to communicate? At the moment I can add the reference, but it is not visible to the code.

Update

I recreated the solution and added the code below, which when I try to compile gives the following error from the Framework452.Tests project:

Error CS0006: Metadata file '~\TryNETStandard\NetStandard12.CentralLibrary\bin\Debug\netstandard1.2\NetStandard12.CentralLibrary.dll' could not be found.

namespace Framework452.Library
{
    public class Returner452 {
        public static bool ReturnTrue() { return true; }
    }
}


using Xunit;
namespace Framework452.Tests
{
    public class Class1 {
        [Fact]
        public void FrameworkTest() {
            Assert.True(NetStandard12.CentralLibrary.Class1.Return452());
        }

        [Fact]
        public void NetStandardTest() {
            Assert.True(NetStandard12.CentralLibrary.Class1.Return12());
        }
    }
}


namespace NetStandard12.BaseLibrary
{
    public class Returner12 {
        public static bool ReturnTrue() { return true; }
    }
}


using Framework452.Library;
using NetStandard12.BaseLibrary;
namespace NetStandard12.CentralLibrary
{
    public class Class1
    {
        public static bool Return452() { return Returner452.ReturnTrue(); }
        public static bool Return12() { return Returner12.ReturnTrue(); }
    }
}
Athari
  • 33,702
  • 16
  • 105
  • 146
Ayb4btu
  • 3,208
  • 5
  • 30
  • 42

2 Answers2

11

No, .NET Standard projects cannot reference framework projects.

.NET Standard projects need to be usable across platforms, forcing a dependency on the .NET framework by referencing an assembly targeting it makes this impossible.

Note that with some of the magic Microsoft is doing with .NET Standard 2.0 this is less true but the overall idea still stands.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
11

According to this page https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-platforms-support you should be able to achieve your purpose because .NET Standard 1.2 support .NET Framework 4.5.1 (UPDATE: This statement is not 100% correct. Please see the Update section below.)

I tried to set up a solution in VS 2017 and set the references as you described. Here is the result.

Solution Explorer

and this is the Class1.cs in NetStandard12.CentralLibrary

Class1.cs

The code compiles fine without any errors.

Note: your code may fail if the Framework452.Library uses an API that is not supported by .NET Standard 1.2 (e.g Winforms, Win32 API or any Microsoft proprietary library that does not make sense for cross platform).

I recommend this youtube playlist on the .NET standard introduction from one of the MSFT https://www.youtube.com/watch?v=YI4MurjfMn8&list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY

In .NET Standard - Checking Compatibilty , he recommended tools to help you find out what API is not supported in the .NET Standard.

Thing will become easier with .NET Standard 2.0 and 'compat shim'

UPDATE:

After trying again with more data provided by the question, it's true that a library targeting (depends) .NET Standard could not depend on a library that target .NET Framework. For some strange reason, the compiler allows me to compile the example that I gave above. This could be a bug in tooling.

After a little more research, I found a good example demonstrate the relationship between NetStandard and NetFramework: How .NET Standard relates to .NET Platform. The graph here show the dependencies Interface Hierarchy According to the graph, there is no way a library that depends on .NET Standard could see/use the .NET framework implementation.

When .NET Standard 2 is released, this may change a little bit and you could reference .NET Framework via Compatibility Shim. See this video for more in-depth explanation https://youtu.be/vg6nR7hS2lI?list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY&t=169

LxL
  • 1,878
  • 2
  • 20
  • 39
  • The setup I had was the same as this, and all that I was doing was have `CentralLibrary` call static methods that would return a `bool` from `BaseLibrary` and `Library`. Then test for these `bool`s in `Tests`. I'll have another go at this a bit later. – Ayb4btu Jul 25 '17 at 01:22
  • Are you using VS 2017? With VS 2017, I don't think you need to reference Nuget meta package for netstandard. The VS 2017 toolset should parse the new project file in .csproj format and does 'dotnet restore' automatically for you. – LxL Jul 25 '17 at 03:29
  • Here is my csproj for NetStandard12.CentralLibrary netstandard1.2 – LxL Jul 25 '17 at 03:31
  • See my update. Before adding in the code above it compiles fine, but now it throws an error. – Ayb4btu Jul 25 '17 at 10:09
  • @Ayb4btu I was wrong, please view my updated answer for more details. – LxL Jul 26 '17 at 04:04