Using xproj and csproj with .NET Core v1 & VS2015

Matt Watson Developer Tips, Tricks & Resources Leave a Comment

.NET Core is here! 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. So what’s the issue? How do you 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.

Another 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.

Another issue 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.

 


Free Download

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

About Matt Watson

Matt is the Founder & CEO of Stackify. He has been a developer/hacker for over 15 years and loves solving hard problems with code. While working in IT management he realized how much of his time was wasted trying to put out production fires without the right tools. He founded Stackify in 2012 to create an easy to use set of tools for developers.