Day 2 - Taking a Look at the Visual Studio Templates for .NET Core
Disclaimer: I am not on the .NET Core Team. I used the tools available publicly.
The working source code for this project can be found here.
Intro
A complete list of post in this series is included below :
- Day 1 - Installing and Running .NET Core on a Windows Box
- Day 2 - Taking a Look at the Visual Studio Templates for .NET Core
- Day 3 - Running a .NET Core app on a Mac
- Day 4 - Creating a NuGet Package from .NET Core app
- Day 5 - Creating a Test Project from .NET Core
- Day 6 - Migrating an existing .NET Core to csproj
- Day 7 - Creating an ASP.NET Core Web Application
- Day 8 - Using Visual Studio Code with a .NET Core Console Application
- Day 9 - Creating a .NET Core Console App inside of Visual Studio Code
- Day 10 - Using JetBrains Rider with a .NET Core Console Application
In this post, I’m going to walk you through the Visual Studio templates for .NET Core.
VS Templates and funky references
Before proceeding make sure that you install .NET Tools for Visual Studio from the downloads page.
Go to File -> New Project and look for .NET Core to create a project in Visual Studio. Select Console Application from the list below:
Once the project loads, you will see the following structure which is very similar to most Console apps with the exception being the way References is displayed.
If we add Console.WriteLine("Hello World");
to the Main method, then we don’t see any red squiggly lines. This is because System.Console is part of the .NET Standard Library that is included in this template as shown below:
You can browse out to the source code to see foundation class libraries in .NET Core. You can also check out Package search to help you find .NET Core class libraries.
What if you need to add a package reference that isn’t available by default?
Let’s setup a situation where the package isn’t included and we’ll need to add it. I’ll use a quick and dirty sample of Json.NET.
Create a file named Account.cs and drop this in:
And add the following to your Main method in Program.cs
Simple enough! The JsonConvert call and Formatting should have a red squiggly line. You can now simply add the package as shown below:
Nice! It has been added. You can look inside project.json and see it being referenced as shown below.
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
The thing to keep in mind here is that you could have actually added the dependency reference directly in the project.json file as shown below:
This is very similar to Node.js if you are familiar with that.
Wrap-up
I’ll call it a day with that. As always, thanks for reading and smash one of those share buttons to give this post some love if you found it helpful. Also, feel free to leave a comment below if there is something that you want to know as I learn .NET Core.
Leave a Comment