Learn the steps to upgrade your .NET core to SDK Preview3 or 1.0.1+ version – and debug your application in Visual Studio Code.
If you use .NET Core SDK 1.0.0-preview3-{buildnumber} , .NET Core no longer uses project.json to handle project and framework references.
Instead, it has moved to a more general approach, using a .csproj file. One of the main reasons to migrate to .csproj is to allow your .NET Core code to interop with existing .NET code.
According to the Microsoft .NET Core team:
“When we started building .NET Core and ASP.NET Core it was important to have a project system that worked across Windows, Mac and Linux and worked in editors other than Visual Studio. The new project.json project format was created to facilitate this. Feedback from customers was they loved the new project.json model, but they wanted their projects to be able to work with existing .NET code they already had. In order to do this we are making .NET Core become .csproj/MSBuild based so it can interop with existing .NET projects and we are taking the best features of project.json and moving them into .csproj/MSBuild…”
To upgrade, there are multiple steps. In fact, in Visual Studio Code, there are additional steps.
In our #TechTuesday Tips series, we want to show you the four steps (five for some) to upgrade from project.json in Visual Studio Preview for Mac.
#1 Compiling Your Application
Those of you who feel adventurous, had an already working .NET Core or ASP .NET Core application and decided to migrate to the latest .NET Core SDK (or at least Preview3), you will notice that your application no longer compiles when you debug the application in Visual Studio Code or run:
dotnet build
As you can see, the error is now a MSBuild error. This happens because your application has project.json instead of the new *.csproj file that MSBuild understands, so what can we do? Well the answer is pretty simple, we have to run one command from .NET Core Command Line Tools.
#2 Migrating Your Application
The command that will take care of converting your project.json project file into *.csproj project file is:
dotnet migrate
Migrate command will create a backup folder and will place your project.json file here . You can opt to not create a backup folder by choosing the skip-backup option.
For a list of available options use:
dotnet migrate — help
NOTE: If you want to update your .NET Framework and .NET Standard version (CLR) make sure to update your project.json file before running the migrate command and update netcoreapp to 1.1 and Microsoft.NETCore.App to 1.1.1
After you run the migrate command, you will notice – if you didn’t choose the skip-backup option – a new folder called backup that contains your project.json file. You will also notice that a new file created – at the root of your project – has a .csproj extension. This is the file that MSBuild looks for to compile your project.
Now that we have the missing file created, MSBuild should no longer complain and our project is supposed to compile successfully. But we might not be correct.
#3 Unsuccessful Compilation, Again
If you run the build command:
dotnet build
You’ll notice that your application compiles successfully . (If you are on a Mac or Linux, you might need to run the command using elevated rights. If you find yourself in this situation, try to run: sudo dotnet build)
Now that we have a successful compilation, we can execute:
dotnet run
Copy the URL on your browser and you’ll see that your application works as expected.
Now, let’s debug the application using Visual Studio Code, it should work right? Well … it won’t.
You’ll see that MSBuild complains again that the project file does not exist. But didn’t we just migrate our project and create the project file?
Well, it turns out that Visual Studio Code uses two files to compile and run the application . These two files are located under .vscode folder. The files: tasks.json and launch.json.
#4 Updating Visual Studio Code Files
Let’s open tasks.json file first, locate the reference to project.json and update it to point to your new .csproj file.
Finally, if you updated the framework version – in project.json before running dotnet migrate – open launch.json and modify the program property.
Make sure to update the path from netcoreapp1.0 to netcoreapp1.1. Otherwise, your application will run using the previously compiled assembly and you might be wondering why your latest updates are not reflected.
#5 File Access Error
If you followed all the steps and you get a file access error when you want to debug your application, then you will have to grant read/write access to your folders . Or run Visual Studio as an Administrator.
If you are using OSX:
- Right click on your project folder
- Click on Get Info
- Under Sharing & Permissions for Staff select Read & Write
- Click on the lock icon, enter the user and password of an admin account
- Click on the gear icon at the bottom and click on Apply to enclosed items
Now debug your application and Voila! Visual Studio Code can now debug your application.