Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing between NetStandard and .Net Framework

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(); }
    }
}
like image 685
Ayb4btu Avatar asked Jul 24 '17 23:07

Ayb4btu


People also ask

Can I reference .NET standard from .NET framework?

NET standard, being a specification, is like an interface. Whenever you add a reference to a . NET standard library, as long as the reference meets the specification definition, then the library doesn't care what type it is. Implementation is separate from specification.

Can you reference .NET 6 from .NET framework?

It will not work, but directly reference the . NET 6 assembly may work(instead of installing and referencing the NuGet package). Note, even if directly referencing the assembly works, it may also cause errors during developing and building.

Can I reference .NET standard from .NET Core?

NET Standard 2.0. That means you can reference the logic library not only from a . NET Core app, but also from an app built for . NET Framework or Xamarin.

What is difference between .NET Core and .NET framework?

NET Core is written from scratch to make it a modular, lightweight, fast, and cross-platform Framework Whereas, Microsoft's Net Framework is a software development platform for building and running Windows applications.Net framework includes developer tools, programming languages, and libraries that are used to develop ...


1 Answers

According to this page https://docs.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

like image 66
LxL Avatar answered Oct 26 '22 07:10

LxL