title | description | keywords | author | manager | ms.date | ms.topic | ms.prod | ms.technology | ms.devlang | ms.assetid |
---|---|---|---|---|---|---|---|---|---|---|
Getting started with .NET Core on Windows |
Getting started with .NET Core on Windows, using Visual Studio 2015 |
.NET, .NET Core |
bleroy |
wpickett |
06/20/2016 |
article |
.net-core |
.net-core-technologies |
dotnet |
d743134a-08a3-4ff6-aab7-49f71f0568c3 |
by Bertrand Le Roy and Phillip Carter
Visual Studio 2015 provides a full-featured development environment for developing .NET Core applications. The procedures in this document describe the steps necessary to build a number of typical .NET Core solutions, or solutions that include .NET Core components, using Visual Studio. The scenarios include testing and using third-party libraries that have not been explicitly built for the most recent version of .NET Core.
-
Visual Studio 2015 Update 3. If you don't have Visual Studio already, you can download Visual Studio Community 2015 for free.
-
NuGet Manager extension for Visual Studio. NuGet is the package manager for the Microsoft development platform including .NET Core. When you use NuGet to install a package, it copies the library files to your solution and automatically updates your project (add references, change config files, etc.).
-
.NET Core Tooling Preview 2 for Visual Studio 2015. This installs templates and other tools for Visual Studio 2015, as well as .NET Core 1.0 itself.
-
A supported version of the Windows client or server operating system. For a list of supported versions, see .NET Core Release Notes.
The following steps will set up Visual Studio 2015 for .NET Core development:
-
Verify that you're running Visual Studio 2015 Update 3:
-
On the Help menu, choose About Microsoft Visual Studio.
-
In the About Microsoft Visual Studio dialog, the version number should include "Update 3" (or higher).
-
If Update 3 (or higher) is not already installed, you can download Visual Studio 2015 Update 3.
-
-
Download and install the MSI Installer for .NET Core Tooling Preview 2. This will install the .NET Core Tooling Preview 2 for Visual Studio 2015, which provides both .NET Core 1.0 and the Visual Studio tool set for .NET Core 1.0.
-
Download and install NuGet Manager extension for Visual Studio. This will install the latest version of the extension.
-
Open Visual Studio, and on the File menu, choose New, Project.
-
In the New Project dialog, in the Templates list, expand the Visual C# node and choose .NET Core. You should see three new project templates for Class Library (.NET Core), Console Application (.NET Core), and ASP.NET Core Web Application (.NET Core).
-
In Visual Studio, choose File, New, Project. In the New Project dialog, expand the Visual C# and .NET Core nodes, and choose Class Library (.NET Core).
-
Name the project "Library" and the solution "Golden". Leave Create directory for solution checked. Click OK.
-
In Solution Explorer, open the context menu for the References node and choose Manage NuGet Packages.
-
Choose "nuget.org" as the Package source, and choose the Browse tab. Check the Including prerelease checkbox, and then browse for Newtonsoft.Json. Click Install.
-
Open the context menu for the References node and choose Restore packages.
-
Rename the
Class1.cs
file toThing.cs
. Accept the rename of the class. Remove the constructor and add a method:public int Get(int number) => Newtonsoft.Json.JsonConvert.DeserializeObject<int>($"{number}");
-
On the Build menu, choose Build Solution.
The solution should build without error.
-
In Solution Explorer, open the context menu for the Solution node and choose Add, New Solution Folder. Name the folder "test". This is only a solution folder, not a physical folder.
-
Open the context menu for the test folder and choose Add. New Project. In the New Project dialog, choose Console Application (.NET Core). Name it "TestLibrary" and explicitly put it under the
Golden\test
path.Important
The project needs to be a console application, not a class library.
-
In the TestLibrary project, open the context menu for the References node and choose Add Reference.
-
In the Reference Manager dialog, check Library under the Projects, Solution node, and then click OK.
-
In the TestLibrary project, open the
project.json
file, and replace"Library": "1.0.0-*"
with"Library": {"target": "project", "version": "1.0.0-*"}
.This is to avoid the resolution of the
Library
project to a NuGet package with the same name. Explicitly setting the target to "project" ensures that the tooling will first search for a project with that name, and not a package. -
In the TestLibrary project, open the context menu for the References node and choose Restore Packages.
-
Open the context menu for the References node and choose Manage NuGet Packages.
-
Choose "nuget.org" as the Package source, and choose the Browse tab. Check the Including prerelease checkbox, and then browse for xUnit version 2.1.0, and then click Install.
-
Browse for dotnet-test-xunit version 1.0.0-rc2 or newer, and then click Install.
-
Edit
project.json
and replace"imports": "dnxcore50"
with"imports": [ "dnxcore50", "portable-net45+win8" ]
.
This enables the xunit libraries to be correctly restored and used by the project: those libraries have been compiled to be used with portable profiles that include "portable-net45+win8", but not .NET Core, which didn't exist when they were built. The import
relaxes the tooling version checks at build time. You may now restore packages without error.
-
Edit
project.json
to add"testRunner": "xunit",
after the"frameworks"
section. -
Add a
LibraryTests.cs
class file to the TestLibrary project, add theusing
directivesusing Xunit;
andusing Library;
to the top of the file, and add the following code to the class:[Fact] public void ThingGetsObjectValFromNumber() { Assert.Equal(42, new Thing().Get(42)); }
- Optionally, delete the
Program.cs
file from the TestLibrary project, and remove"buildOptions": {"emitEntryPoint": true}
fromproject.json
.
- Optionally, delete the
You should now be able to build the solution.
- On the Test menu, choose Windows, Test Explorer, and in Test Explorer choose Run All.
The test should pass.
-
In Solution Explorer, open the context menu for the
src
folder, and add a new Console Application (.NET Core) project. Name it "App", and set the location toGolden\src
. -
In the App project, open the context menu for the References node and choose Add, Reference.
-
In the Reference Manager dialog, check Library under the Projects, Solution node, and then click OK
-
Open the context menu for the References node and choose Restore Packages.
-
Open the context menu for the App node and choose Set as StartUp Project.
-
Open the
Program.cs
file, add ausing Library;
directive to the top of the file, and then addConsole.WriteLine($"The answer is {new Thing().Get(42)}");
to theMain
method. -
Set a breakpoint after the line that you just added.
-
Press F5 to run the application..
The application should build without error, and should hit the breakpoint. You should also be able to check that the application output "The answer is 42.".
Starting from the solution obtained with the previous script, execute the following steps:
-
In Solution Explorer, open the
project.json
file for the Library project and replace"frameworks": { "netstandard1.5"
with"frameworks": { "netstandard1.4"
. -
In the Library project, open the context menu for the References node and choose Restore Packages.
The solution should still build and function exactly like it did before: the test should pass, and the console application should run and be debuggable.
-
In Solution Explorer, open the context menu for the
src
folder, and choose Add. , New Project. -
In the New Project dialog, choose the Visual C# node, and then choose Console Application.
Important
Make sure you choose a standard console application, not the .NET Core version. In this section, you'll be consuming the library from a .NET Framework application
-
Name the project "FxApp", and set the location to
Golden\src
. -
In the FxApp project, open the the context menu for the References node and choose Add Reference.
-
In the Reference Manager dialog, choose Browse and browse to the location of the built
Library.dll
(under the ..Golden\src\Library\bin\Debug\netstandard1.4 path), and then click Add.You could also package the library and reference the package, as another way to reference .NET Core code from the .NET Framework.
-
Open the context menu for the References node and choose Manage NuGet Packages.
-
Choose "nuget.org" as the Package source, and choose the Browse tab. Check the Including prerelease checkbox, and then browse for Newtonsoft.Json. Click Install.
-
In the FxApp project, open the
Program.cs
file and add ausing Library;
directive to the top of the file, and addConsole.WriteLine($"The answer is {new Thing().Get(42)}.");
to theMain
method of the program. -
Set a breakpoint after the line that you just added.
-
Make FxApp the startup application for the solution.
-
Press F5 to run the app.
The application should build and hit the breakpoint. The application output should be "The answer is 42.".
-
In Solution Explorer, open the
project.json
file in the Library project. -
Replace
frameworks": { "netstandard1.4"
withframeworks": { "netstandard1.3"
. -
In the Library project, open the context menu for the References node and choose Restore Packages.
-
On the Build menu, choose Build Library.
-
Remove the
Library
reference from the FxApp then add it back using the ..Golden\src\Library\bin\Debug\netstandard1.3 path. This will now reference the 1.3 version. -
Press F5 to run the application.
Everything should still work as it did before. Check that the application output is "The answer is 42.", that the breakpoint was hit, and that variables can be inspected.
Close the previous solution if it was open: you will be starting a new script from this section on.
-
In Visual Studio, choose File, New, Project. In the New Project dialog, expand the Visual C# node, and choose Class Library (Portable for iOS, Android and Windows).
-
Name the project "PCLLibrary" and the solution "GoldenPCL". Leave Create directory for solution checked. Click OK.
-
In Solution Explorer, open the context menu for the References node and choose Manage NuGet Packages.
-
Choose "nuget.org" as the Package source, and choose the Browse tab. Check the Including prerelease checkbox, and then browse for Newtonsoft.Json. Click Install.
-
Rename the class "Thing" and add a method:
public int Get(int number) => Newtonsoft.Json.JsonConvert.DeserializeObject<int>($"{number}");
-
On the Build menu, choose Build Solution, and verify that the solution builds.
-
In Solution Explorer, open the contaxt menu for the Solution 'GoldenPCL' node and choose Add. New Project. In the New Project dialog, expand the Visual C# node, choose Console Application, and name the project "App".
-
In the App project, open the context menu for the References node and choose Add, Reference.
-
In the Reference Manager dialog, choose Browse and browse to the location of the built
PCLLibrary.dll
(under the ..\GoldenPCL\PCLLibrary\bin\Debug path), and then click Add. -
In the App project, open the
Program.cs
file and add ausing PCLLibrary;
directive to the top of the file, and addConsole.WriteLine($"The answer is {new Thing().Get(42)}.");
to theMain
method of the program. -
Set a breakpoint after the line that you just added..
-
In Solution Explorer, open the context menu for the App node and choose Set as StartUp Project.
-
Press F5 to run the app.
The application should build, run, and hit the breakpoint after it outputs "The answer is 42.".
The PCL library that we built in the previous procedure is based on a csproj
project file. In order to move it to NetStandard, the simplest solution is to manually move its code into a new empty .NET Core Class Library project.
If you have older PCL libraries with a xproj
file and a project.json
file, you should be able to edit the project.json
file instead, to reference "NETStandard.Library": "1.6.0"
, and target "netstandard1.3".