Skip to content

Commit 679ebbe

Browse files
.NET 5 updates (#21964)
Co-authored-by: David Pine <[email protected]>
1 parent 0cd6ba7 commit 679ebbe

File tree

4 files changed

+57
-76
lines changed

4 files changed

+57
-76
lines changed

docs/core/tutorials/libraries.md

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
2-
title: Develop libraries with the .NET Core CLI
3-
description: Learn how to create .NET Core libraries using the .NET Core CLI. You'll create a library that supports multiple frameworks.
2+
title: Develop libraries with the .NET CLI
3+
description: Learn how to create .NET libraries using the .NET CLI. You'll create a library that supports multiple frameworks.
44
author: cartermp
55
ms.topic: how-to
66
ms.date: 05/01/2017
77
---
8-
# Develop libraries with the .NET Core CLI
8+
# Develop libraries with the .NET CLI
99

10-
This article covers how to write libraries for .NET using the .NET Core CLI. The CLI provides an efficient and low-level experience that works across any supported OS. You can still build libraries with Visual Studio, and if that is your preferred experience [refer to the Visual Studio guide](library-with-visual-studio.md).
10+
This article covers how to write libraries for .NET using the .NET CLI. The CLI provides an efficient and low-level experience that works across any supported OS. You can still build libraries with Visual Studio, and if that is your preferred experience [refer to the Visual Studio guide](library-with-visual-studio.md).
1111

1212
## Prerequisites
1313

14-
You need [the .NET Core SDK and CLI](https://dotnet.microsoft.com/download) installed on your machine.
14+
You need [the .NET SDK and CLI](https://dotnet.microsoft.com/download) installed on your machine.
1515

1616
For the sections of this document dealing with .NET Framework versions, you need the [.NET Framework](https://dotnet.microsoft.com) installed on a Windows machine.
1717

@@ -27,35 +27,27 @@ Additionally, if you wish to support older .NET Framework targets, you need to i
2727
| 4.0 | Windows SDK for Windows 7 and .NET Framework 4 |
2828
| 2.0, 3.0, and 3.5 | .NET Framework 3.5 SP1 Runtime (or Windows 8+ version) |
2929

30-
## How to target .NET Standard
30+
## How to target .NET 5.0 or .NET Standard
3131

32-
If you're not familiar with .NET Standard, refer to [.NET Standard](../../standard/net-standard.md) to learn more.
32+
You control your project's target framework by adding it to your project file (*.csproj* or *.fsproj*). For guidance on how to choose between targeting .NET 5.0 or .NET Standard see [.NET 5 and .NET Standard](../../standard/net-standard).
3333

34-
In that article, there is a table that maps .NET Standard versions to various implementations:
35-
36-
[!INCLUDE [net-standard-table](../../../includes/net-standard-table.md)]
37-
38-
Here's what this table means for the purposes of creating a library:
39-
40-
The version of .NET Standard you pick will be a tradeoff between access to the newest APIs and the ability to target more .NET implementations and .NET Standard versions. You control the range of targetable platforms and versions by picking a version of `netstandardX.X` (where `X.X` is a version number) and adding it to your project file (`.csproj` or `.fsproj`).
41-
42-
You have three primary options when targeting .NET Standard, depending on your needs.
43-
44-
1. You can use the default version of .NET Standard supplied by templates, `netstandard1.4`, which gives you access to most APIs on .NET Standard while still being compatible with UWP, .NET Framework 4.6.1, and .NET Standard 2.0.
45-
46-
```xml
47-
<Project Sdk="Microsoft.NET.Sdk">
48-
<PropertyGroup>
49-
<TargetFramework>netstandard1.4</TargetFramework>
50-
</PropertyGroup>
51-
</Project>
52-
```
53-
54-
2. You can use a lower or higher version of .NET Standard by modifying the value in the `TargetFramework` node of your project file.
34+
```xml
35+
<Project Sdk="Microsoft.NET.Sdk">
36+
<PropertyGroup>
37+
<TargetFramework>net5.0</TargetFramework>
38+
</PropertyGroup>
39+
</Project>
40+
```
5541

56-
.NET Standard versions are backward compatible. That means that `netstandard1.0` libraries run on `netstandard1.1` platforms and higher. However, there is no forward compatibility. Lower .NET Standard platforms cannot reference higher ones. This means that `netstandard1.0` libraries cannot reference libraries targeting `netstandard1.1` or higher. Select the Standard version that has the right mix of APIs and platform support for your needs. We recommend `netstandard1.4` for now.
42+
```xml
43+
<Project Sdk="Microsoft.NET.Sdk">
44+
<PropertyGroup>
45+
<TargetFramework>netstandard2.0</TargetFramework>
46+
</PropertyGroup>
47+
</Project>
48+
```
5749

58-
3. If you want to target .NET Framework versions 4.0 or below, or you wish to use an API available in .NET Framework but not in .NET Standard (for example, `System.Drawing`), read the following sections and learn how to multitarget.
50+
If you want to target .NET Framework versions 4.0 or below, or you wish to use an API available in .NET Framework but not in .NET Standard (for example, `System.Drawing`), read the following sections and learn how to multitarget.
5951

6052
## How to target .NET Framework
6153

@@ -98,7 +90,7 @@ And that's it! Although this compiled only for .NET Framework 4, you can use the
9890
> [!NOTE]
9991
> The following instructions assume you have the .NET Framework installed on your machine. Refer to the [Prerequisites](#prerequisites) section to learn which dependencies you need to install and where to download them from.
10092
101-
You may need to target older versions of the .NET Framework when your project supports both the .NET Framework and .NET Core. In this scenario, if you want to use newer APIs and language constructs for the newer targets, use `#if` directives in your code. You also might need to add different packages and dependencies for each platform you're targeting to include the different APIs needed for each case.
93+
You may need to target older versions of the .NET Framework when your project supports both the .NET Framework and .NET. In this scenario, if you want to use newer APIs and language constructs for the newer targets, use `#if` directives in your code. You also might need to add different packages and dependencies for each platform you're targeting to include the different APIs needed for each case.
10294

10395
For example, let's say you have a library that performs networking operations over HTTP. For .NET Standard and the .NET Framework versions 4.5 or higher, you can use the `HttpClient` class from the `System.Net.Http` namespace. However, earlier versions of the .NET Framework don't have the `HttpClient` class, so you could use the `WebClient` class from the `System.Net` namespace for those instead.
10496

@@ -107,7 +99,7 @@ Your project file could look like this:
10799
```xml
108100
<Project Sdk="Microsoft.NET.Sdk">
109101
<PropertyGroup>
110-
<TargetFrameworks>netstandard1.4;net40;net45</TargetFrameworks>
102+
<TargetFrameworks>netstandard2.0;net40;net45</TargetFrameworks>
111103
</PropertyGroup>
112104

113105
<!-- Need to conditionally bring in references for the .NET Framework 4.0 target -->
@@ -201,17 +193,17 @@ If you build this project with `dotnet build`, you'll notice three directories u
201193
```
202194
net40/
203195
net45/
204-
netstandard1.4/
196+
netstandard2.0/
205197
```
206198

207-
Each of these contain the `.dll` files for each target.
199+
Each of these contains the `.dll` files for each target.
208200

209-
## How to test libraries on .NET Core
201+
## How to test libraries on .NET
210202

211-
It's important to be able to test across platforms. You can use either [xUnit](https://xunit.github.io/) or MSTest out of the box. Both are perfectly suitable for unit testing your library on .NET Core. How you set up your solution with test projects will depend on the [structure of your solution](#structuring-a-solution). The following example assumes that the test and source directories live in the same top-level directory.
203+
It's important to be able to test across platforms. You can use either [xUnit](https://xunit.github.io/) or MSTest out of the box. Both are perfectly suitable for unit testing your library on .NET. How you set up your solution with test projects will depend on the [structure of your solution](#structuring-a-solution). The following example assumes that the test and source directories live in the same top-level directory.
212204

213205
> [!NOTE]
214-
> This uses some [.NET Core CLI](../tools/index.md) commands. See [dotnet new](../tools/dotnet-new.md) and [dotnet sln](../tools/dotnet-sln.md) for more information.
206+
> This uses some [.NET CLI](../tools/index.md) commands. See [dotnet new](../tools/dotnet-new.md) and [dotnet sln](../tools/dotnet-sln.md) for more information.
215207
216208
1. Set up your solution. You can do so with the following commands:
217209

@@ -248,8 +240,6 @@ It's important to be able to test across platforms. You can use either [xUnit](h
248240
dotnet build
249241
```
250242

251-
[!INCLUDE[DotNet Restore Note](../../../includes/dotnet-restore-note.md)]
252-
253243
1. Verify that xUnit runs by executing the `dotnet test` command. If you chose to use MSTest, then the MSTest console runner should run instead.
254244

255245
And that's it! You can now test your library across all platforms using command-line tools. To continue testing now that you have everything set up, testing your library is very simple:
@@ -313,7 +303,7 @@ This will add the three projects above and a solution file that links them toget
313303

314304
### Project-to-project referencing
315305

316-
The best way to reference a project is to use the .NET Core CLI to add a project reference. From the **AwesomeLibrary.CSharp** and **AwesomeLibrary.FSharp** project directories, you can run the following command:
306+
The best way to reference a project is to use the .NET CLI to add a project reference. From the **AwesomeLibrary.CSharp** and **AwesomeLibrary.FSharp** project directories, you can run the following command:
317307

318308
```dotnetcli
319309
dotnet add reference ../AwesomeLibrary.Core/AwesomeLibrary.Core.csproj
@@ -327,7 +317,7 @@ The project files for both **AwesomeLibrary.CSharp** and **AwesomeLibrary.FSharp
327317
</ItemGroup>
328318
```
329319

330-
You can add this section to each project file manually if you prefer not to use the .NET Core CLI.
320+
You can add this section to each project file manually if you prefer not to use the .NET CLI.
331321

332322
### Structuring a solution
333323

docs/core/tutorials/testing-with-cli.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
title: Organizing and testing projects with the .NET Core CLI
3-
description: This tutorial explains how to organize and test .NET Core projects from the command line.
2+
title: Organizing and testing projects with the .NET CLI
3+
description: This tutorial explains how to organize and test .NET projects from the command line.
44
author: cartermp
55
ms.date: 09/10/2018
66
---
77

8-
# Organizing and testing projects with the .NET Core CLI
8+
# Organizing and testing projects with the .NET CLI
99

10-
This tutorial follows [Tutorial: Create a console application with .NET Core using Visual Studio Code](with-visual-studio-code.md), taking you beyond the creation of a simple console app to develop advanced and well-organized applications. After showing you how to use folders to organize your code, this tutorial shows you how to extend a console application with the [xUnit](https://xunit.github.io/) testing framework.
10+
This tutorial follows [Tutorial: Create a console application with .NET using Visual Studio Code](with-visual-studio-code.md), taking you beyond the creation of a simple console app to develop advanced and well-organized applications. After showing you how to use folders to organize your code, this tutorial shows you how to extend a console application with the [xUnit](https://xunit.github.io/) testing framework.
1111

1212
## Using folders to organize code
1313

@@ -38,6 +38,10 @@ Projects that logically group files into folders are easy to navigate and mainta
3838

3939
## Organizing and testing using the NewTypes Pets Sample
4040

41+
### Prerequisites
42+
43+
* [.NET 5.0 SDK](https://dotnet.microsoft.com/download) or a later version.
44+
4145
### Building the sample
4246

4347
For the following steps, you can either follow along using the [NewTypes Pets Sample](https://github.com/dotnet/samples/tree/master/core/console-apps/NewTypesMsBuild) or create your own files and folders. The types are logically organized into a folder structure that permits the addition of more types later, and tests are also logically placed in folders permitting the addition of more tests later.
@@ -176,39 +180,27 @@ The following shows the complete project structure:
176180
|__NewTypesTests.csproj
177181
```
178182

179-
Start in the *test/NewTypesTests* directory. Restore the test project with the [`dotnet restore`](../tools/dotnet-restore.md)
180-
command. Run the tests with the [`dotnet test`](../tools/dotnet-test.md) command. This command starts the test runner specified in the project file.
181-
182-
[!INCLUDE[DotNet Restore Note](~/includes/dotnet-restore-note.md)]
183+
Start in the *test/NewTypesTests* directory. Run the tests with the [`dotnet test`](../tools/dotnet-test.md) command. This command starts the test runner specified in the project file.
183184

184185
As expected, testing fails, and the console displays the following output:
185186

186187
```output
187-
Test run for c:\Users\ronpet\repos\samples\core\console-apps\NewTypesMsBuild\test\NewTypesTests\bin\Debug\netcoreapp2.1\NewTypesTests.dll(.NETCoreApp,Version=v2.1)
188-
Microsoft (R) Test Execution Command Line Tool Version 15.8.0
188+
Test run for C:\Source\dotnet\docs\samples\snippets\core\tutorials\testing-with-cli\csharp\test\NewTypesTests\bin\Debug\net5.0\NewTypesTests.dll (.NETCoreApp,Version=v5.0)
189+
Microsoft (R) Test Execution Command Line Tool Version 16.8.1
189190
Copyright (c) Microsoft Corporation. All rights reserved.
190191
191192
Starting test execution, please wait...
192-
[xUnit.net 00:00:00.77] PetTests.DogTalkToOwnerReturnsWoof [FAIL]
193-
[xUnit.net 00:00:00.78] PetTests.CatTalkToOwnerReturnsMeow [FAIL]
194-
Failed PetTests.DogTalkToOwnerReturnsWoof
195-
Error Message:
196-
Assert.NotEqual() Failure
193+
A total of 1 test files matched the specified pattern.
194+
[xUnit.net 00:00:00.50] PetTests.DogTalkToOwnerReturnsWoof [FAIL]
195+
Failed PetTests.DogTalkToOwnerReturnsWoof [6 ms]
196+
Error Message:
197+
Assert.NotEqual() Failure
197198
Expected: Not "Woof!"
198199
Actual: "Woof!"
199-
Stack Trace:
200-
at PetTests.DogTalkToOwnerReturnsWoof() in c:\Users\ronpet\repos\samples\core\console-apps\NewTypesMsBuild\test\NewTypesTests\PetTests.cs:line 13
201-
Failed PetTests.CatTalkToOwnerReturnsMeow
202-
Error Message:
203-
Assert.NotEqual() Failure
204-
Expected: Not "Meow!"
205-
Actual: "Meow!"
206-
Stack Trace:
207-
at PetTests.CatTalkToOwnerReturnsMeow() in c:\Users\ronpet\repos\samples\core\console-apps\NewTypesMsBuild\test\NewTypesTests\PetTests.cs:line 22
208-
209-
Total tests: 2. Passed: 0. Failed: 2. Skipped: 0.
210-
Test Run Failed.
211-
Test execution time: 1.7000 Seconds
200+
Stack Trace:
201+
at PetTests.DogTalkToOwnerReturnsWoof() in C:\Source\dotnet\docs\samples\snippets\core\tutorials\testing-with-cli\csharp\test\NewTypesTests\PetTests.cs:line 13
202+
203+
Failed! - Failed: 1, Passed: 1, Skipped: 0, Total: 2, Duration: 8 ms - NewTypesTests.dll (net5.0)
212204
```
213205

214206
Change the assertions of your tests from `Assert.NotEqual` to `Assert.Equal`:
@@ -218,15 +210,14 @@ Change the assertions of your tests from `Assert.NotEqual` to `Assert.Equal`:
218210
Re-run the tests with the `dotnet test` command and obtain the following output:
219211

220212
```output
221-
Test run for c:\Users\ronpet\repos\samples\core\console-apps\NewTypesMsBuild\test\NewTypesTests\bin\Debug\netcoreapp2.1\NewTypesTests.dll(.NETCoreApp,Version=v2.1)
222-
Microsoft (R) Test Execution Command Line Tool Version 15.8.0
213+
Test run for C:\Source\dotnet\docs\samples\snippets\core\tutorials\testing-with-cli\csharp\test\NewTypesTests\bin\Debug\net5.0\NewTypesTests.dll (.NETCoreApp,Version=v5.0)
214+
Microsoft (R) Test Execution Command Line Tool Version 16.8.1
223215
Copyright (c) Microsoft Corporation. All rights reserved.
224216
225217
Starting test execution, please wait...
218+
A total of 1 test files matched the specified pattern.
226219
227-
Total tests: 2. Passed: 2. Failed: 0. Skipped: 0.
228-
Test Run Successful.
229-
Test execution time: 1.6029 Seconds
220+
Passed! - Failed: 0, Passed: 2, Skipped: 0, Total: 2, Duration: 2 ms - NewTypesTests.dll (net5.0)
230221
```
231222

232223
Testing passes. The pet types' methods return the correct values when talking to the owner.

samples/snippets/core/tutorials/testing-with-cli/csharp/src/NewTypes/NewTypes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net5.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

samples/snippets/core/tutorials/testing-with-cli/csharp/test/NewTypesTests/NewTypesTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

0 commit comments

Comments
 (0)