Microsoft released .Net Core 1.0 few weeks ago and open source community has started working to add support for new platform into their libraries. I also spent hours on researching, tweaking some of my code to add support for .Net Core into our library. This blog post is about  sharing the experience and learning I had while adding support for .Net Core.

I had following tools installed on my development machine

.NET Core is focused towards enabling .net community for cross platform application development, which allows to develop applications with Windows, Mac and Linux. A lot of architectural changes has been made at the framework level. Also infrastructural changes has been introduced for projects and solutions e.g. a different project type for project files, XPROJ. Build system is project.json centric.

First step was finalizing the strategy that how we are going to add support into our library because there are some breaking changes involved. It’s not as easy as re-targetting your project to different profile. There were two options

  1. Update the library with Conditional code (#if netstandard …) within the same project
  2. Create separate branch of our existing code base, update code to support .net core

Another consideration we made that we can keep the support of the existing project type, .csProj, while add additional support for new project type, .XProj. It was achieved by having two separate solution files, one for the conventional project type and other for new project types.

Real implementation started with the migration of .csproj and packages.config into the new project.json file. First we opened up the existing solution and save it as a different name (mySolution.NetCore). There are multiple approached you will found on the web. To avoid the hand editing of individual config files, We followed following steps for each project in the solution

  • Right clicked and unloaded the project (myProject.csproj)
  • Removed the project from the solution (mySolution.NetCore) since it is a .csproj file and we were planning to add new project type in the solution
  • Opend the same project folder via file explorer and rename the project folder (myproject_1) to some other name
  • Added a new project of .Net Core type (class library, console app etc) with the same name(myProject) in the solution(mySolution.NetCore)
  • Unload newly added project (myProject)
  • Copy all the files from renamed folder into the newly created project folder (myProject_1 to myproject)
  • Reload the project and it should have all the files from old project

After completing above steps, I had two project files for each project, each one is referenced in different solution.  Also you need to focus on project.json file of your project that which framework you want to target. One problem that arose during this process was that excluded files started compiling and causing issues. We can use excludeFiles directive in the project.json. Following is simplified project.json for one of projects.

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "myOtherCLassLibrary": "1.0.0-*",
    "Newtonsoft.Json": "9.0.1"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  },
  "buildOptions": {
    "compile": {
      "excludeFiles": [
        "Helpers/ScheduledWrapper.cs"
      ]
    }
  }
}

Once projects has been migrated, Please don’t forget to test the functionality. I had my unit tests project as well but I couldn’t migrate it because framework support didn’t roled out yet. I read xUnit nuget package is release and we might need to update our test framework but we haven’t finalized anything yet because it will involve loads of effort.