Add new info and fix for trace switch issue

This commit is contained in:
Mark J Price 2023-06-29 15:19:19 +01:00
parent 3bac632114
commit c1c4286dee
2 changed files with 15 additions and 13 deletions

View file

@ -36,9 +36,9 @@ To solve this problem, reboot your computer.
# Microsoft introduces a bug in a later version # Microsoft introduces a bug in a later version
Although rare, it is possible that by using a later version of a NuGet package than the one I used to write the book, you experience different behavior, especially negative behavior if it is due to a bug. Although rare, it is possible that by using a later version of a NuGet package than the one I used to write the book, you experience different behavior, especially negative behavior if it is due to a bug or a fix to a bug.
For example, in the `Microsoft.Extensions.Configuration.Binder` package, versions `7.0.3` and `7.0.4` have a bug that causes an exception to be thrown when it tries to parse a trace level set in an `appsettings.json` file. Previous versions from `7.0.0` to `7.0.2` did not have this bug. For example, in the `Microsoft.Extensions.Configuration.Binder` package, versions `7.0.3` or later fix a bug that causes an exception to be thrown when it tries to parse a trace level set in an `appsettings.json` file. Previous versions `7.0.2` and earlier including .NET 6 did not have this bug fix and so the book code.
You can read more this specific example here: https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/errata.md#page-178---reviewing-project-packages You can read more this specific example here: https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/errata.md#page-178---reviewing-project-packages

View file

@ -273,9 +273,9 @@ But the immediately following section is *Navigating with the debugging toolbar*
# Page 178 - Reviewing project packages # Page 178 - Reviewing project packages
> Thanks to [Nick Bettes](https://github.com/bettesn) and [Zhang Cheng](https://github.com/Matrix-Zhang) for raising this issue on [16 February 2023](https://github.com/markjprice/cs11dotnet7/issues/29), and a special thanks to [Huynh Loc Le](https://github.com/huynhloc-1110), who identified that the issue was caused by a Microsoft bug. > Thanks to [Nick Bettes](https://github.com/bettesn) and [Zhang Cheng](https://github.com/Matrix-Zhang) for raising this issue on [16 February 2023](https://github.com/markjprice/cs11dotnet7/issues/29), a special thanks to [Huynh Loc Le](https://github.com/huynhloc-1110), who identified that the issue was caused by a Microsoft bug, and finally thanks to [richshi](https://github.com/richshi) who raised this issue again on [28 June 2023](https://github.com/markjprice/cs11dotnet7/issues/76) and consequently made me dig deeper to find a more complete explanation and solution.
In Step 1, you add package references to enable an `appsettings.json` file to configure a trace switch. If you reference `Microsoft.Extensions.Configuration.Binder` package versions `7.0.3` or `7.0.4`, they have a bug that causes an exception to be thrown, as shown in the following output: In Step 1, you add package references to enable an `appsettings.json` file to configure a trace switch. If you reference `Microsoft.Extensions.Configuration.Binder` package versions `7.0.3` or later, then they have fixed an ancient bug, but the fix means that unless we change how we set the trace switch level an exception will be thrown, as shown in the following output:
``` ```
Reading from appsettings.json in C:\cs11dotnet7\Chapter04\Instrumenting\bin\Debug\net7.0 Reading from appsettings.json in C:\cs11dotnet7\Chapter04\Instrumenting\bin\Debug\net7.0
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
@ -286,18 +286,20 @@ Unhandled exception. System.Reflection.TargetInvocationException: Exception has
at System.Diagnostics.TraceSwitch.OnValueChanged() at System.Diagnostics.TraceSwitch.OnValueChanged()
``` ```
Until Microsoft fixes the bug, use version `7.0.2`, the latest version that works correctly, as shown in the following markup: Originally, it seemed the only fix was to avoid later versions, so use 7.0.2 or earlier. But the change in 7.0.3 was to fix a bug, so we need a solution that will allow us to use the latest package version and also works for older versions too.
```xml
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<!--The following packages are included anyway due to dependencies--> To fix the issue, we must set both the `Level` property and the `Value` property of the trace switch, as shown in the following code:
<!--<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />--> ```json
<!--<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />--> {
</ItemGroup> "PacktSwitch": {
"Value": "Info", // Must be set to work with 7.0.3 or later.
"Level": "Info" // To work with 7.0.2 or earlier including .NET 6.
}
}
``` ```
> See the following explanation from Microsoft about the bug they fixed that cause this problem: https://github.com/dotnet/runtime/issues/82998
See also this common mistake item: https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/common-errors.md#microsoft-introduces-a-bug-in-a-later-version See also this common mistake item: https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/common-errors.md#microsoft-introduces-a-bug-in-a-later-version
# Page 180 - Reviewing project packages # Page 180 - Reviewing project packages