Create and consume a .Net Standard Library in .Net Framework and .Net Core Applications

.Net Standard Library is a specification through which API portability is achieved on all .Net runtimes. It defines a set of base class libraries which are compatible with all .Net platforms like Windows, UWP, .Net Core, .Net Framework, Windows Phone etc. Using BCL’s, developers can write custom portable API and use them across different .Net platforms.

Below table (reference from Docs.Microsoft.com) shows different .Net runtimes and their highest supported .Net Standard version. So if UWP supports .Net Standard 1.5, that means it supports the consolidated API of .Net standards from 1.0 to 1.5. .Net Standard libraries are in a way similar to Portable Class Libraries (which are also used for portability). The major difference is that PCL’s are based on profiles where as .Net Standard libraries are agnostic to platform and are curated API’s (but not intersection of existing platforms).

image

The main component of .Net Standard is NETStandard.Library. The NETStandard.Library is a meta-package which describes the set of libraries that present in one or more .NET Standard Library versions. A custom class library which is intended to target .Net Standard should use netstandard target framework moniker (for example netstandard1.6). We can reference NETStandard.Library which provides access to the different .NET libraries and associated APIs that define the .NET Standard Library. We can also refer additional packages that target netstandard target framework moniker.

Lets get started by creating  a custom .Net Standard Library using Visual Studio Community 2015 Update 3. Create a Class Library (Portable) project with name NetStandardClassLibrary.

image

Select all the default targets.

image

The following VS Solution structure will be created.

image

Open the Project Properties, we should see all the targets.

image

To support .Net Standard, click on Target .NET Platform Standard. We get following warning message, click yes.

image

Now we have Class Library targeting .Net Standard.

image

Because of above change VS Solution will be changed as shown below. Project.json is the major file which got added and .Net Standard dependencies are automatically added as references.

image

Lets assume that our Library has to support .Net Core, .Net Framework 4.6.1 and UWP 10 platforms, then as per the above table we need to use netstandard1.4. So lets change the project.json as shown below. We mention the framework as netstandard1.4 and the libraries to be fetched from NetStandard.Library 1.6.0.

{
  "supports": {},
  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.4": {}
  }
}

For testing, add the below class with Hello method which will return hello string.

public class Class1
{
    public string Hello()
    {
        return "Hello";
    }
}

Build the solution and we should get the DLL output. Now lets build a nuget package out of the project. Open command line prompt, navigate to the project folder and finally run dotnet pack command.

image

Nuget package files are created under bin/Debug folder (I am running on Debug configuration).

image

Upload the generated package to nuget.org as shown below.

image

And on Verify Details section, click on submit to confirm package upload.

image

On successful submission, we get our package details displayed as shown below. Nuget clearly identifies the dependency of NetStandard.Library 1.6.0.

image

Now lets test the DLL with both .Net Core 1 and .Net Framework 4.6.1 applications. I created a .Net Core 1 Console application and added the our custom NetStandard library as below in project.json.

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    },
    "NetStandardClassLibrary" :  "1.0.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

We can include the API in Program class as shown below.

public class Program
{
    public static void Main(string[] args)
    {
        NetStandardClassLibrary.Class1 netStandardClass = new NetStandardClassLibrary.Class1();
        Console.WriteLine(netStandardClass.Hello());
        Console.ReadLine();
    }
}

Run the program and we see below output.

image

Now lets create an .Net Framework 4.6.1 Console application and add nuget package as shown below.

image

All the related dependent packages from .Net Standard 1.4 will be installed along with NetStandardClassLibrary as shown below.

image

Now we can use the API in Program class as shown below.

public class Program
{
    public static void Main(string[] args)
    {
        NetStandardClassLibrary.Class1 netStandardClass = new NetStandardClassLibrary.Class1();
        Console.WriteLine(netStandardClass.Hello());
        Console.ReadLine();
    }
}

When we run the application we will have following output.

image

Similarly we can test with UWP applications as well. This way we can share the .Net Standard library between different .Net Runtimes.

That’s it for now, Happy Coding and Stay Tuned!!!

You may also like...