Search code examples
c#.net.net-core.net-core-rc2.net-standard-1.5

how to create a .net core class library


I am new to .net core. I need some help to configure my project.json file to create a class library with .net core. I actually read a lot of documents, but I am lost since there is an overwhelming amount of things to read. I know that I have to use the NETStandardLibray (https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md), but I don't know how to put this in my config.json file.

Here is my config.json file:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    },
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-build10025"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },
  "testRunner": "xunit"
}

I want to build a simple class library to be reused in another projects, and I also want it to have unit tests.

My main difficulty here is about the monikers. They are such a mess. I just want to build a class library that can be used in any platform, but it looks like that I have to choose the platform in my project.json anyways. In the list of deprecated monikers I don't know what moniker I should use. In order to use the NET Standard Library, should I target the platform .NET Standard Application 1.5 that has the nuget identifier netcoreapp1.0, or should I use the platform .NET Platform 5.0 that has the nuget identifier netstandard1.3??? What is the correct platform? Wasn't NETStandard Library supposed to be platform agnostic? And should I put those identifiers in the dependencies or in the frameworks section of my project.json? And whats the difference of those two section at all? I'm totally lost :(


Solution

  • Your class library itself should have a framework of netstandard1.3 or whatever version you want - Noda Time is targeting netstandard1.3 for example, as I've found that there's too much missing from netstandard1.0, unfortunately. I'd generally pick the lowest version you're confident in. You don't want netcoreapp1.0 etc, as you're writing a library, not an application.

    Your unit tests should be in a separate project, configured as per the xUnit documentation - for example, targeting netcoreapp1.0 and maybe net451 to run on the desktop framework too.

    To answer your other concerns, while the .NET Standard Library is platform agnostic (in terms of operating system), you still need to express what version you depend on - if you need something that's only in netstandard1.5, you can't target netstandard1.3, for example.

    The part about including Microsoft.NETCore.App etc in the dependencies section is (as I understand it) to avoid having to specify every dependency individually in a modular way. It's a simple way to get started, but you might later want to be more specific, trimming your dependencies to only those you need.

    There's definitely a lot still up in the air, a lot of documentation still to be written, and a lot of now-out-of-date documentation and blog posts from the last couple of years... but progress is being made.