Skip to content

Commit

Permalink
Bump versions to .NET 6 and upgrade C# code to v10 (dotnet#27051)
Browse files Browse the repository at this point in the history
* TourOfCsharp first

* Workers

* Caching

* Config

* Globbing

* Http

* Loc

* Primitives

* Fix loc range ref

* Added file-scoped namespace preference

* Converting to 'id' instead of range, figured out we can delimit multiples.

* More range fixes

* Revert settings.json change

* More fixes

* Options bits fixed

* Even more range/id ::: battles

* C# 10-ify

* OMG, so many things are broken -- GRRR!

* I think that's all of it now

* Manually spot checked all extensions/fundamentals updates

* Show DI example with top level statments.

* Another refactor

* Fix off-by-one error

* Update docs/core/extensions/logging-providers.md
  • Loading branch information
IEvangelist authored Nov 18, 2021
1 parent d1e06a3 commit bf74489
Show file tree
Hide file tree
Showing 214 changed files with 2,454 additions and 2,756 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# Namespace preference
csharp_style_namespace_declarations = file_scoped

# Types: use keywords instead of BCL types, and permit var only when the type is clear
csharp_style_var_for_built_in_types = false:suggestion
csharp_style_var_when_type_is_apparent = false:none
Expand Down
24 changes: 12 additions & 12 deletions docs/core/extensions/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Caching in .NET
description: Learn how to use various in-memory and distributed caching mechanisms in .NET.
author: IEvangelist
ms.author: dapine
ms.date: 07/26/2021
ms.date: 11/12/2021
---

# Caching in .NET
Expand Down Expand Up @@ -49,19 +49,19 @@ Setting an expiration will cause entries in the cache to be *evicted* if they're

To use the default <xref:Microsoft.Extensions.Caching.Memory.IMemoryCache> implementation, call the <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddMemoryCache%2A> extension method to register all the required services with DI. In the following code sample, the generic host is used to expose the <xref:Microsoft.Extensions.Hosting.HostBuilder.ConfigureServices%2A> functionality:

:::code source="snippets/caching/memory-apis/Program.cs" range="3-9" highlight="6":::
:::code source="snippets/caching/memory-apis/Program.cs" range="1-7" highlight="6":::

Depending on your .NET workload, you may access the `IMemoryCache` differently; such as constructor injection. In this sample, you use the `IServiceProvider` instance on the `host` and call generic <xref:Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService%60%601(System.IServiceProvider)> extension method:

:::code source="snippets/caching/memory-apis/Program.cs" range="11-12":::
:::code source="snippets/caching/memory-apis/Program.cs" range="9-10":::

With in-memory caching services registered, and resolved through DI &mdash; you're ready to start caching. This sample iterates through the letters in the English alphabet 'A' through 'Z'. There is a `record` that holds the reference to the letter, and generates a message.

:::code source="snippets/caching/memory-apis/Program.cs" range="70-74":::
:::code source="snippets/caching/memory-apis/Program.cs" range="68-72":::

The sample includes a helper function that iterates through the alphabet letters:

:::code source="snippets/caching/memory-apis/Program.cs" range="26-35":::
:::code source="snippets/caching/memory-apis/Program.cs" range="24-33":::

In the preceding C# code:

Expand All @@ -70,7 +70,7 @@ In the preceding C# code:

To add items to the cache call one of the `Create`, or `Set` APIs:

:::code source="snippets/caching/memory-apis/Program.cs" range="37-55" highlight="12-13":::
:::code source="snippets/caching/memory-apis/Program.cs" range="35-53" highlight="12-13":::

In the preceding C# code:

Expand All @@ -86,11 +86,11 @@ For each letter in the alphabet, a cache entry is written with an expiration, an

The post eviction callback writes the details of the value that was evicted to the console:

:::code source="snippets/caching/memory-apis/Program.cs" range="17-24":::
:::code source="snippets/caching/memory-apis/Program.cs" range="15-22":::

Now that the cache is populated, another call to `IterateAlphabetAsync` is awaited, but this time you'll call <xref:Microsoft.Extensions.Caching.Memory.IMemoryCache.TryGetValue%2A?displayProperty=nameWithType>:

:::code source="snippets/caching/memory-apis/Program.cs" range="57-66":::
:::code source="snippets/caching/memory-apis/Program.cs" range="55-64":::

If the `cache` contains the `letter` key, and the `value` is an instance of an `AlphabetLetter` it's written to the console. When the `letter` key is not in the cache, it was evicted and its post eviction callback was invoked.

Expand Down Expand Up @@ -188,7 +188,7 @@ Imagine you're developing a photo service that relies on third-party API accessi

In the following example, you'll see several services being registered with DI. Each service has a single responsibility.

:::code source="snippets/caching/memory-worker/Program.cs" range="1-18":::
:::code source="snippets/caching/memory-worker/Program.cs" range="1-14":::

In the preceding C# code:

Expand Down Expand Up @@ -270,9 +270,9 @@ To create values in the distributed cache, call one of the set APIs:

Using the `AlphabetLetter` record from the in-memory cache example, you could serialize the object to JSON and then encode the `string` as a `byte[]`:

:::code source="snippets/caching/distributed/Program.cs" range="41-51" highlight="7-9":::
:::code source="snippets/caching/distributed/Program.cs" id="Create" highlight="7-9":::

Much like in-memory caching, cache entries can have options to help fine tune their existence in the cache &mdash; in this case, the <xref:Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions>.
Much like in-memory caching, cache entries can have options to help fine-tune their existence in the cache &mdash; in this case, the <xref:Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions>.

##### Create extension methods

Expand All @@ -288,7 +288,7 @@ To read values from the distributed cache, call one of the get APIs:
- <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache.GetAsync%2A?displayProperty=nameWithType>
- <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache.Get%2A?displayProperty=nameWithType>

:::code source="snippets/caching/distributed/Program.cs" range="61-67" highlight="5-6":::
:::code source="snippets/caching/distributed/Program.cs" id="Read" highlight="5-6":::

Once a cache entry is read out of the cache, you can get the UTF8 encoded `string` representation from the `byte[]`

Expand Down
28 changes: 14 additions & 14 deletions docs/core/extensions/configuration-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Configuration providers in .NET
description: Learn how the Configuration provider API is used to configure .NET applications.
author: IEvangelist
ms.author: dapine
ms.date: 05/21/2021
ms.date: 11/12/2021
ms.topic: reference
---

Expand Down Expand Up @@ -36,14 +36,14 @@ Overloads can specify:

Consider the following code:

:::code language="csharp" source="snippets/configuration/console-json/Program.cs" range="1-39,43-44" highlight="23-29":::
:::code language="csharp" source="snippets/configuration/console-json/Program.cs" range="1-37,43-44" highlight="21-27":::

The preceding code:

- Clears all existing configuration providers that were added by default in the <xref:Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(System.String[])> method.
- Configures the JSON configuration provider to load the *appsettings.json* and *appsettings*.`Environment`.*json* files with the following options:
- `optional: true`: The file is optional.
- `reloadOnChange: true` : The file is reloaded when changes are saved.
- `reloadOnChange: true`: The file is reloaded when changes are saved.

The JSON settings are overridden by settings in the [Environment variables configuration provider](#environment-variable-configuration-provider) and the [Command-line configuration provider](#command-line-configuration-provider).

Expand All @@ -62,26 +62,26 @@ Consider the `TransientFaultHandlingOptions` class defined as follows:

The following code builds the configuration root, binds a section to the `TransientFaultHandlingOptions` class type, and prints the bound values to the console window:

:::code language="csharp" source="snippets/configuration/console-json/Program.cs" range="31-38":::
:::code language="csharp" source="snippets/configuration/console-json/Program.cs" range="29-36":::

The application would write the following sample output:

:::code language="csharp" source="snippets/configuration/console-json/Program.cs" range="40-42":::
:::code language="csharp" source="snippets/configuration/console-json/Program.cs" id="Output":::

### XML configuration provider

The <xref:Microsoft.Extensions.Configuration.Xml.XmlConfigurationProvider> class loads configuration from an XML file at run time. Install the [`Microsoft.Extensions.Configuration.Xml`](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Xml) NuGet package.

The following code demonstrates configuration of XML files using the XML configuration provider.

:::code language="csharp" source="snippets/configuration/console-xml/Program.cs" range="1-34,52,58-59" highlight="23-34":::
:::code language="csharp" source="snippets/configuration/console-xml/Program.cs" range="1-32,50,58-59" highlight="21-32":::

The preceding code:

- Clears all existing configuration providers that were added by default in the <xref:Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(System.String[])> method.
- Configures the XML configuration provider to load the *appsettings.xml* and *repeating-example.xml* files with the following options:
- `optional: true`: The file is optional.
- `reloadOnChange: true` : The file is reloaded when changes are saved.
- `reloadOnChange: true`: The file is reloaded when changes are saved.
- Configures the environment variables configuration provider.
- Configures the command-line configuration provider if the given `args` contains arguments.

Expand All @@ -97,11 +97,11 @@ In .NET 5 and earlier versions, add the `name` attribute to distinguish repeatin

The following code reads the previous configuration file and displays the keys and values:

:::code language="csharp" source="snippets/configuration/console-xml/Program.cs" range="36-51":::
:::code language="csharp" source="snippets/configuration/console-xml/Program.cs" range="34-49":::

The application would write the following sample output:

:::code language="csharp" source="snippets/configuration/console-xml/Program.cs" range="53-57":::
:::code language="csharp" source="snippets/configuration/console-xml/Program.cs" id="Output":::

Attributes can be used to supply values:

Expand All @@ -126,19 +126,19 @@ The <xref:Microsoft.Extensions.Configuration.Ini.IniConfigurationProvider> class

The following code clears all the configuration providers and adds the `IniConfigurationProvider` with two INI files as the source:

:::code language="csharp" source="snippets/configuration/console-ini/Program.cs" range="1-37,44-45" highlight="24-30":::
:::code language="csharp" source="snippets/configuration/console-ini/Program.cs" range="1-34,43-44" highlight="21-27":::

An example *appsettings.ini* file with various configuration settings follows:

:::code language="ini" source="snippets/configuration/console-ini/appsettings.ini":::

The following code displays the preceding configuration settings by writing them to the console window:

:::code language="csharp" source="snippets/configuration/console-ini/Program.cs" range="32-36":::
:::code language="csharp" source="snippets/configuration/console-ini/Program.cs" range="29-33":::

The application would write the following sample output:

:::code language="csharp" source="snippets/configuration/console-ini/Program.cs" range="38-43":::
:::code language="csharp" source="snippets/configuration/console-ini/Program.cs" id="Output":::

## Environment variable configuration provider

Expand Down Expand Up @@ -185,7 +185,7 @@ To test that the preceding commands override *appsettings.json* and *appsettings

Call <xref:Microsoft.Extensions.Configuration.EnvironmentVariablesExtensions.AddEnvironmentVariables%2A> with a string to specify a prefix for environment variables:

:::code language="csharp" source="snippets/configuration/console-env/Program.cs" highlight="21-22":::
:::code language="csharp" source="snippets/configuration/console-env/Program.cs" highlight="20-21":::

In the preceding code:

Expand Down Expand Up @@ -312,7 +312,7 @@ The <xref:Microsoft.Extensions.Configuration.Memory.MemoryConfigurationProvider>

The following code adds a memory collection to the configuration system:

:::code language="csharp" source="snippets/configuration/console-memory/Program.cs" highlight="22-29":::
:::code language="csharp" source="snippets/configuration/console-memory/Program.cs" highlight="20-27":::

In the preceding code, <xref:Microsoft.Extensions.Configuration.MemoryConfigurationBuilderExtensions.AddInMemoryCollection(Microsoft.Extensions.Configuration.IConfigurationBuilder,System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{System.String,System.String}})?displayProperty=nameWithType> adds the memory provider after the default configuration providers. For an example of ordering the configuration providers, see [XML configuration provider](#xml-configuration-provider).

Expand Down
4 changes: 2 additions & 2 deletions docs/core/extensions/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Configuration in .NET
description: Learn how to use the Configuration API to configure .NET applications.
author: IEvangelist
ms.author: dapine
ms.date: 09/16/2020
ms.date: 11/12/2021
ms.topic: overview
---

Expand All @@ -24,7 +24,7 @@ Configuration in .NET is performed using one or more [configuration providers](#

New .NET console applications created using [dotnet new](../tools/dotnet-new.md) or Visual Studio by default *do not* expose configuration capabilities. To add configuration in a new .NET console application, [add a package reference](../tools/dotnet-add-package.md) to `Microsoft.Extensions.Hosting`. Modify the *Program.cs* file to match the following code:

:::code language="csharp" source="snippets/configuration/console/Program.cs" highlight="18":::
:::code language="csharp" source="snippets/configuration/console/Program.cs" highlight="17":::

The <xref:Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(System.String[])?displayProperty=nameWithType> method provides default configuration for the app in the following order:

Expand Down
10 changes: 5 additions & 5 deletions docs/core/extensions/console-log-formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Console log formatting
description: Learn how to use available console log formatting, or implement custom log formatting for your .NET applications.
author: IEvangelist
ms.author: dapine
ms.date: 07/14/2021
ms.date: 11/12/2021
---

# Console log formatting
Expand Down Expand Up @@ -71,7 +71,7 @@ info: Microsoft.Hosting.Lifetime[0]

By default, the `Simple` console log formatter is selected with default configuration. You change this by calling `AddJsonConsole` in the *Program.cs*:

:::code language="csharp" source="snippets/logging/console-formatter-json/Program.cs" highlight="17-26":::
:::code language="csharp" source="snippets/logging/console-formatter-json/Program.cs" highlight="14-22":::

Run the app again, with the above change, the log message is now formatted as JSON:

Expand Down Expand Up @@ -103,7 +103,7 @@ To implement a custom formatter, you need to:

Create an extension method to handle this for you:

:::code language="csharp" source="snippets/logging/console-formatter-custom/ConsoleLoggerExtensions.cs" highlight="11-12":::
:::code language="csharp" source="snippets/logging/console-formatter-custom/ConsoleLoggerExtensions.cs" highlight="10-11":::

The `CustomOptions` are defined as follows:

Expand All @@ -121,7 +121,7 @@ The `AddConsoleFormatter` API:

Define a `CustomerFormatter` subclass of `ConsoleFormatter`:

:::code language="csharp" source="snippets/logging/console-formatter-custom/CustomFormatter.cs" highlight="24-45":::
:::code language="csharp" source="snippets/logging/console-formatter-custom/CustomFormatter.cs" highlight="22-38":::

The preceding `CustomFormatter.Write<TState>` API dictates what text gets wrapped around each log message. A standard `ConsoleFormatter` should be able to wrap around scopes, time stamps, and severity level of logs at a minimum. Additionally, you can encode ANSI colors in the log messages, and provide desired indentations as well. The implementation of the `CustomFormatter.Write<TState>` lacks these capabilities.

Expand Down Expand Up @@ -185,7 +185,7 @@ Next, write some extension methods in a `TextWriterExtensions` class that allow

A custom color formatter that handles applying custom colors could be defined as follows:

:::code language="csharp" source="snippets/logging/console-formatter-custom/CustomColorFormatter.cs" highlight="15-18,52-65":::
:::code language="csharp" source="snippets/logging/console-formatter-custom/CustomColorFormatter.cs" highlight="13-16,50-63":::

When you run the application, the logs will show the `CustomPrefix` message in the color green when `FormatterOptions.ColorBehavior` is `Enabled`.

Expand Down
8 changes: 4 additions & 4 deletions docs/core/extensions/custom-configuration-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Implement a custom configuration provider in .NET
description: Learn how to implement a custom configuration provider in .NET applications.
author: IEvangelist
ms.author: dapine
ms.date: 04/19/2021
ms.date: 11/12/2021
ms.topic: how-to
---

Expand All @@ -20,7 +20,7 @@ The provider has the following characteristics:
- The EF in-memory database is used for demonstration purposes.
- To use a database that requires a connection string, get a connection string from an interim configuration.
- The provider reads a database table into configuration at startup. The provider doesn't query the database on a per-key basis.
- Reload-on-change isn't implemented, so updating the database after the app starts has no effect on the app's configuration.
- Reload-on-change isn't implemented, so updating the database after the app has started will not affect the app's configuration.

Define a `Settings` record type entity for storing configuration values in the database. For example, you could add a *Settings.cs* file in your *Models* folder:

Expand Down Expand Up @@ -59,7 +59,7 @@ An `AddEntityConfiguration` extension method permits adding the configuration so
The following code shows how to use the custom `EntityConfigurationProvider` in *Program.cs*:

:::code language="csharp" source="snippets/configuration/custom-provider/Program.cs" highlight="33":::
:::code language="csharp" source="snippets/configuration/custom-provider/Program.cs" highlight="31":::

## Consume provider

Expand All @@ -69,7 +69,7 @@ To consume the custom configuration provider, you can use the [options pattern](

A call to <xref:Microsoft.Extensions.Hosting.IHostBuilder.ConfigureServices%2A> configures the mapping of the options.

:::code language="csharp" source="snippets/configuration/custom-provider/Program.cs" highlight="16-19,35-37":::
:::code language="csharp" source="snippets/configuration/custom-provider/Program.cs" highlight="14-17,33-35":::

The preceding code configures the `WidgetOptions` object from the `"WidgetOptions"` section of the configuration. This enables the options pattern, exposing a dependency injection-ready `IOptions<WidgetOptions>` representation of the EF settings. The options are ultimately provided from the custom configuration provider.

Expand Down
Loading

0 comments on commit bf74489

Please sign in to comment.