You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/tutorials/libraries.md
+31-41Lines changed: 31 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
---
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.
4
4
author: cartermp
5
5
ms.topic: how-to
6
6
ms.date: 05/01/2017
7
7
---
8
-
# Develop libraries with the .NET Core CLI
8
+
# Develop libraries with the .NET CLI
9
9
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).
11
11
12
12
## Prerequisites
13
13
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.
15
15
16
16
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.
17
17
@@ -27,35 +27,27 @@ Additionally, if you wish to support older .NET Framework targets, you need to i
27
27
| 4.0 | Windows SDK for Windows 7 and .NET Framework 4 |
28
28
| 2.0, 3.0, and 3.5 | .NET Framework 3.5 SP1 Runtime (or Windows 8+ version) |
29
29
30
-
## How to target .NET Standard
30
+
## How to target .NET 5.0 or .NET Standard
31
31
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).
33
33
34
-
In that article, there is a table that maps .NET Standard versions to various implementations:
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
-
<ProjectSdk="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
+
<ProjectSdk="Microsoft.NET.Sdk">
36
+
<PropertyGroup>
37
+
<TargetFramework>net5.0</TargetFramework>
38
+
</PropertyGroup>
39
+
</Project>
40
+
```
55
41
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
+
<ProjectSdk="Microsoft.NET.Sdk">
44
+
<PropertyGroup>
45
+
<TargetFramework>netstandard2.0</TargetFramework>
46
+
</PropertyGroup>
47
+
</Project>
48
+
```
57
49
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.
59
51
60
52
## How to target .NET Framework
61
53
@@ -98,7 +90,7 @@ And that's it! Although this compiled only for .NET Framework 4, you can use the
98
90
> [!NOTE]
99
91
> 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.
100
92
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.
102
94
103
95
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.
104
96
@@ -107,7 +99,7 @@ Your project file could look like this:
<!-- 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
201
193
```
202
194
net40/
203
195
net45/
204
-
netstandard1.4/
196
+
netstandard2.0/
205
197
```
206
198
207
-
Each of these contain the `.dll` files for each target.
199
+
Each of these contains the `.dll` files for each target.
208
200
209
-
## How to test libraries on .NET Core
201
+
## How to test libraries on .NET
210
202
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.
212
204
213
205
> [!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.
215
207
216
208
1. Set up your solution. You can do so with the following commands:
217
209
@@ -248,8 +240,6 @@ It's important to be able to test across platforms. You can use either [xUnit](h
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.
254
244
255
245
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
313
303
314
304
### Project-to-project referencing
315
305
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:
Copy file name to clipboardExpand all lines: docs/core/tutorials/testing-with-cli.md
+24-33Lines changed: 24 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
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.
4
4
author: cartermp
5
5
ms.date: 09/10/2018
6
6
---
7
7
8
-
# Organizing and testing projects with the .NET Core CLI
8
+
# Organizing and testing projects with the .NET CLI
9
9
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.
11
11
12
12
## Using folders to organize code
13
13
@@ -38,6 +38,10 @@ Projects that logically group files into folders are easy to navigate and mainta
38
38
39
39
## Organizing and testing using the NewTypes Pets Sample
40
40
41
+
### Prerequisites
42
+
43
+
*[.NET 5.0 SDK](https://dotnet.microsoft.com/download) or a later version.
44
+
41
45
### Building the sample
42
46
43
47
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:
176
180
|__NewTypesTests.csproj
177
181
```
178
182
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.
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.
183
184
184
185
As expected, testing fails, and the console displays the following output:
185
186
186
187
```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
189
190
Copyright (c) Microsoft Corporation. All rights reserved.
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
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`:
218
210
Re-run the tests with the `dotnet test` command and obtain the following output:
219
211
220
212
```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
223
215
Copyright (c) Microsoft Corporation. All rights reserved.
224
216
225
217
Starting test execution, please wait...
218
+
A total of 1 test files matched the specified pattern.
0 commit comments