Using both xproj and csproj with .NET Core v1 & VS2015

NET-Core-Projs

.NET Core is here and we have been busy doing quite a bit of work with it. We have multiple client libraries for sending data to the Stackify API (for metrics, errors, logs, etc.). To ensure that all of our customers can send us data for their .NET Core apps, we have ported them over. We have also been working on porting Prefix to .NET Core so it can run on a Mac!

The big problem has been how to do we use both csproj and xproj? We need csproj so we can build our apps with our existing toolchain, which relies on msbuild. We also need to reference some shared libraries across multiple different apps. Luckily, with Core 1.0 you can use both csproj and xproj at the same time by having different solution files.

A major obstacle is the project.json file. This file is new for most developers working with .NET Core, but it isn’t actually new (it is also being utilized by universal Windows apps). If you add a project.json file to your application folder, your csproj will start trying to use it. It actually works great and can replace your packages.config and do a good job of setting project package dependencies for non core apps. The big problem you run into is as soon as you add a .NET Core framework target to project.json, your csproj won’t know what the heck to do it with it.

 

Enter the Workaround!

The way to fix this is to have two “project.json” files:

  1. The first file is a csproj that basically does nothing. Name it something like “projectname.project.json.”
  2. The second file should be named the normal project.json for your actual Core app. Luckily there is some hackery in msbuild and the Visual Studio tools that allows this to work.

Here is how our this sample project “Profiler.Models” looks on disk:

xproj-csproj-files

In this example, Profiler.Models.project.json is picked up by our csproj project. Our xproj for .NET Core properly reads the “project json,” and both can compile and build correctly. MSBuild can still build our normal solution with all our csproj based projects. Mission accomplished!

Here is what your “Profiler.Models.project.json” type file should contain. It is just a simple placeholder config.

{  “runtimes”: {    “win”: {}  },  “frameworks”: {    “net45”: {}  }}

I did see a little oddity when trying to add a new package dependency to my csproj. It added it to my Profiler.Models.project.json and things got a little weird, so watch out for that. I ended up deleting all my project.json files in the folder temporarily, adding the package to my csproj and then reverting my project.json deletes via git so they came right back. I kept my Profiler.Models.project.json clean and empty. You might be able to actually use it to manage all the dependencies for your cspoj.

Both Prefix & Retrace APM work with .NET Core!

 

Stackify Retrace APM for .NET Core Released

 

Matt-1

 

Matt Watson

Founder & CEO / Stackify