Skip to content

Commit

Permalink
Add dotnet 8 support (#804)
Browse files Browse the repository at this point in the history
* Update editor.config

* update to use dotnet 8

* fix analyzer warnings

* Update docs
  • Loading branch information
edumserrano authored Nov 26, 2023
1 parent daf5788 commit 69aa315
Show file tree
Hide file tree
Showing 72 changed files with 300 additions and 572 deletions.
59 changes: 29 additions & 30 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,22 @@ dotnet_diagnostic.CA2007.severity = none # CA2007: Do not directly await
dotnet_diagnostic.CA2234.severity = silent # CA2234: Pass System.Uri objects instead of strings

##########################################
# Language Rules
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules
# .NET code refactoring options
# https://learn.microsoft.com/en-us/visualstudio/ide/reference/code-styles-refactoring-options?view=vs-2022
##########################################
# .NET Style Rules
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules#net-style-rules
[*.cs]
# Parentheses preferences
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Expression-level preferences
dotnet_diagnostic.IDE0010.severity = silent # Add missing cases to switch statement (IDE0010)
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_diagnostic.IDE0045.severity = suggestion # Use conditional expression for assignment (IDE0045)
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_diagnostic.IDE0046.severity = none # Use conditional expression for return (IDE0046)
# Undocumented
dotnet_style_operator_placement_when_wrapping = beginning_of_line

# C# Style Rules
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules#c-style-rules
##########################################
# Language and unnecessary rules
# https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/style-rules/language-rules
##########################################
[*.cs]
# 'var' preferences
dotnet_diagnostic.IDE0008.severity = none # IDE0008: Use explicit type instead of 'var'
csharp_style_var_for_built_in_types = true:warning
csharp_style_var_when_type_is_apparent = true:warning
csharp_style_var_elsewhere = true:warning
# Expression-bodied members

# Code-block preferences https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/style-rules/language-rules#code-block-preferences
dotnet_diagnostic.IDE0290.severity = silent # Use primary constructor (IDE0290)

# Expression-bodied members https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/style-rules/language-rules#expression-bodied-members
csharp_style_expression_bodied_methods = true:silent
dotnet_diagnostic.IDE0022.severity = silent # Use expression body for methods (IDE0022)
csharp_style_expression_bodied_operators = true:silent
Expand All @@ -144,18 +134,27 @@ csharp_style_expression_bodied_lambdas = true:silent
dotnet_diagnostic.IDE0053.severity = silent # Use expression body for lambdas (IDE0053)
csharp_style_expression_bodied_local_functions = true:silent
dotnet_diagnostic.IDE0061.severity = silent # Use expression body for local functions (IDE0061)
# Expression-level preferences

# Expression-level preferences https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/style-rules/language-rules#expression-level-preferences
dotnet_diagnostic.IDE0010.severity = silent # Add missing cases to switch statement (IDE0010)
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_diagnostic.IDE0045.severity = suggestion # Use conditional expression for assignment (IDE0045)
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_diagnostic.IDE0046.severity = none # Use conditional expression for return (IDE0046)
dotnet_diagnostic.IDE0057.severity = none # Use range operator (IDE0057)
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
dotnet_diagnostic.IDE0058.severity = silent # Remove unnecessary expression value (IDE0058)
csharp_style_implicit_object_creation_when_type_is_apparent = false:silent
dotnet_diagnostic.IDE0090.severity = suggestion # Simplify new expression (IDE0090)

##########################################
# Unnecessary Code Rules
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/unnecessary-code-rules
##########################################
[*.cs]
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
dotnet_diagnostic.IDE0058.severity = silent # Remove unnecessary expression value (IDE0058)
# Parentheses preferences https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/style-rules/language-rules#parentheses-preferences
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# 'var' preferences https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/style-rules/language-rules#var-preferences
dotnet_diagnostic.IDE0008.severity = none # IDE0008: Use explicit type instead of 'var'
csharp_style_var_for_built_in_types = true:warning
csharp_style_var_when_type_is_apparent = true:warning
csharp_style_var_elsewhere = true:warning

##########################################
# Formatting Rules
Expand Down Expand Up @@ -498,7 +497,7 @@ dotnet_diagnostic.SA1006.severity = suggestion # SA1006: Preprocessor keywords
dotnet_diagnostic.SA1007.severity = suggestion # SA1007: Operator keyword should be followed by space
dotnet_diagnostic.SA1008.severity = suggestion # SA1008: Opening parenthesis should be spaced correctly
dotnet_diagnostic.SA1009.severity = suggestion # SA1009: Closing parenthesis should be spaced correctly
dotnet_diagnostic.SA1010.severity = suggestion # SA1010: Opening square brackets should be spaced correctly
dotnet_diagnostic.SA1010.severity = none # SA1010: Opening square brackets should be spaced correctly # TODO review this, temporarily disabled until https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3687 is resolved. Hopefully soon by merging https://github.com/DotNetAnalyzers/StyleCopAnalyzers/pull/3729
dotnet_diagnostic.SA1011.severity = suggestion # SA1011: Closing square brackets should be spaced correctly
dotnet_diagnostic.SA1012.severity = suggestion # SA1012: Opening braces should be spaced correctly
dotnet_diagnostic.SA1013.severity = suggestion # SA1013: Closing braces should be spaced correctly
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
with:
dotnet-version: |
6.0.x
7.0.x
global-json-file: global.json
- name: Cache/Restore NuGets
uses: actions/cache@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
with:
dotnet-version: |
6.0.x
7.0.x
global-json-file: global.json
- name: Cache/Restore NuGets
uses: actions/cache@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/dotnet-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
with:
dotnet-version: |
6.0.x
7.0.x
global-json-file: global.json
- name: Cache/Restore NuGets
uses: actions/cache@v3
Expand Down
4 changes: 2 additions & 2 deletions docs/configuration/options-without-IOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You will have to add the [dotnet-sdk-extensions](https://www.nuget.org/packages/

## How to use

> **Note**
> [!NOTE]
>
> the variable `services` in the examples below is of type `IServiceCollection`. On the default template
> for a Web API you can access it via `builder.services`. Example:
Expand Down Expand Up @@ -79,7 +79,7 @@ services
.ValidateDataAnnotations();
```

> **Note**
> [!NOTE]
>
> The `AddOptionsValue` extension methods add the ability to take a dependency on `SomeOption`, they don't remove remove the ability to take a dependency on `IOptions<SomeOption>`.
>
2 changes: 1 addition & 1 deletion docs/dev-notes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

The test projects run against multiple frameworks and the [workflow to build and test](/.github/workflows/nuget-publish.yml) the solution runs both on Linux and on Windows.

> **Note**
> [!NOTE]
>
> Some tests run a test server with an HTTPS URL so you have to run the following command to trust developer certificates:
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This workflow requires the following labels to be configured on the repo:

- `dotnet-format`: Pull requests created by the dotnet-format-apply-changes workflow.

> **Note**
> [!NOTE]
>
> The reason to split this workflow in two, one that runs dotnet format `(dotnet-format)` and this one that applies the results `(dotnet-format-apply-changes)` is due to security. On GitHub, workflows that run on PRs from forks of the repo run in a restricted context without access to secrets and where the `GITHUB_TOKEN` has read-only permissions. The main purpose for this is to protect from the threat of malicious pull requests.
>
Expand Down
2 changes: 1 addition & 1 deletion docs/dev-notes/workflows/dotnet-format-workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

The `dotnet format` will report violations based on the [.editorconfig](/.editorconfig) file and the analyzers included in each project. Note that in addition to the analyzers each `csproj` has, the [Directory.Build.props](/docs/dev-notes/README.md#projects-wide-configuration) file also adds several analyzers to the projects.

> **Note**
> [!NOTE]
>
> The reason to split this workflow in two, this one that runs dotnet format `(dotnet-format)` and one that applies the results `(dotnet-format-apply-changes)` is due to security. On GitHub, workflows that run on PRs from forks of the repo run in a restricted context without access to secrets and where the `GITHUB_TOKEN` has read-only permissions. The main purpose for this is to protect from the threat of malicious pull requests.
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If the markdown-link-check workflow was executed on a pull request branch:
- If there aren't any broken markdown links then the workflow stops.
- If there are broken markdown links then a comment with information about the broken links will be added to the Pull Request.

> **Note**
> [!NOTE]
>
> The reason to split this workflow in two, one that looks for broken links `(markdown-link-check)` and this one that processes the results `(markdown-link-check-handle-result)` is due to security. On GitHub, workflows that run on PRs from forks of the repo run in a restricted context without access to secrets and where the `GITHUB_TOKEN` has read-only permissions. The main purpose for this is to protect from the threat of malicious pull requests.
>
Expand Down
2 changes: 1 addition & 1 deletion docs/dev-notes/workflows/markdown-link-check-workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

When the workflow completes, it uploads a workflow info artifact that contains a summary `json` file which indicates if the there are any broken links as well as some other required info that will be further processed by the [markdown-link-check-handle-result workflow](/docs/dev-notes/workflows/markdown-link-check-handle-result-workflow.md).

> **Note**
> [!NOTE]
>
> The reason to split this workflow in two, this one that looks for broken links `(markdown-link-check)` and one that processes the results `(markdown-link-check-handle-result)` is due to security. On GitHub, workflows that run on PRs from forks of the repo run in a restricted context without access to secrets and where the `GITHUB_TOKEN` has read-only permissions. The main purpose for this is to protect from the threat of malicious pull requests.
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Because this workflow requires a `GITHUB_TOKEN` with some write permissions this workflow has been separated from the [nuget-publish workflow](/docs/dev-notes/workflows/nuget-publish-workflow.md). The main intent is to protect from the threat of malicious pull requests. For more information see [Security considerations on GitHub workflows](/docs/dev-notes/workflows/security-considerations.md).

> **Note**
> [!NOTE]
>
> The reason to split this workflow in two, one that produces the test results `(build-test-package)` and this one which uploads them to the pull request, is due to security. On GitHub, workflows that run on PRs from forks of the repo run in a restricted context without access to secrets and where the `GITHUB_TOKEN` has read-only permissions. The main purpose for this is to protect from the threat of malicious pull requests.
>
Expand Down
2 changes: 1 addition & 1 deletion docs/dev-notes/workflows/upload-coverage-to-codecov.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This workflow uses a custom secret `CODECOV_TOKEN`. This secret contains a [toke
- [Upload Issues (`Unable to locate build via Github Actions API`)](https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954)
- [Error: failed to properly upload](https://github.com/codecov/codecov-action/issues/598)

> **Note**
> [!NOTE]
>
> If the above issue is resolved then the use of the codecov action can be simplified by removing the secret and this workflow can be merged into the [build-test-package workflow](/docs/dev-notes/workflows/build-test-package-workflow.md).
>
Expand Down
6 changes: 3 additions & 3 deletions docs/integration-tests/hosted-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public async Task DemoTest2()

The above is a very simple example that hopefully gives you an idea on how you can do the integratin style tests for Hosted Services.

> **Note**
> [!NOTE]
>
> You can also consider converting your project to start a web application and follow the instructions at [Test hosted service using WebApplicationFactory](#test-hosted-service-using-webapplicationfactory).
>
Expand Down Expand Up @@ -354,7 +354,7 @@ The above is a very simple example that hopefully gives you an idea on how you c

The main difference from the integration test examples shown in [introduction to integration tests](https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?#introduction-to-integration-tests) is that you do not use the `WebApplicationFactory.CreateClient()` and then use the returned HttpClient do to calls into the test server but instead you use the `WebApplicationFactory.RunUntilAsync` extension method with a custom conditions that will control the lifetime of the test server when using Hosted Services.

> **Note**
> [!NOTE]
>
> When thinking about your test scenario understand that your code running on your Hosted Service won't immediatly stop when the test condition is reached. In reality, the set condition is checked periodically to understand if the test server should be stopped.
>
Expand Down Expand Up @@ -436,4 +436,4 @@ Setting the `RunUntilOptions.PredicateCheckInterval` to high values might mean y

So if for your test it will take X time to meet the condition and the `RunUntilOptions.PredicateCheckInterval` is represented by Y than in the worst case scenario the time to run your test will be close to X + Y.

**Note**: when debugging it might be useful to set the `RunUntilOptions.PredicateCheckInterval` to a larger period to allow you to step through your code more easily before the check for the condition kicks in and, if evaluates to true, shuts down the test server and ends the test.
[!NOTE]: when debugging it might be useful to set the `RunUntilOptions.PredicateCheckInterval` to a larger period to allow you to step through your code more easily before the check for the condition kicks in and, if evaluates to true, shuts down the test server and ends the test.
2 changes: 1 addition & 1 deletion docs/integration-tests/http-mocking-in-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class HttpMocksDemoTests : IClassFixture<WebApplicationFactory<Progam>>
}
```

**Note**: the above test assumes that there is a typed client, represented by the type `IMyApiClient`, added to the `IServiceCollection` of the `Progam` class through the `IServiceCollection.AddHttpClient` method.
[!NOTE]: the above test assumes that there is a typed client, represented by the type `IMyApiClient`, added to the `IServiceCollection` of the `Progam` class through the `IServiceCollection.AddHttpClient` method.

## Mock different types of HttpClients

Expand Down
2 changes: 1 addition & 1 deletion docs/integration-tests/web-application-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ public class CustomWebApplicationFactory : WebApplicationFactory<SomeTypeInMyTes
}
```

**Note**: the call to `ConfigureWebHostDefaults` in the method `CreateHostBuilder` is likely required if you're testing web apps because it will register default services usually required by web apps. For example: `IServiceCollection.AddRouting`.
[!NOTE]: the call to `ConfigureWebHostDefaults` in the method `CreateHostBuilder` is likely required if you're testing web apps because it will register default services usually required by web apps. For example: `IServiceCollection.AddRouting`.
2 changes: 1 addition & 1 deletion docs/polly/circuit-breaker-checker-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You will have to add the [dotnet-sdk-extensions](https://www.nuget.org/packages/

## How to use

> **Note**
> [!NOTE]
>
> the variable `services` in the examples below is of type `IServiceCollection`. On the default template
> for a Web API you can access it via `builder.services`. Example:
Expand Down
2 changes: 1 addition & 1 deletion docs/polly/extending-policy-options-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ All of the options from the following extension methods have a default validatio

Let's see how we can extend the validation of the `TimeoutOptions` from the [`AddTimeoutPolicy` extension method](/docs/polly/httpclient-with-timeout-policy.md). The same can be applied to the options of any of the other extension methods.

> **Note**
> [!NOTE]
>
> the variable `services` in the examples below is of type `IServiceCollection`. On the default template
> for a Web API you can access it via `builder.services`. Example:
Expand Down
8 changes: 7 additions & 1 deletion docs/polly/httpclient-with-circuit-breaker-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
- [Handling events from the circuit breaker policy](#handling-events-from-the-circuit-breaker-policy)
- [Note about the circuit breaker checker policy](#note-about-the-circuit-breaker-checker-policy)

> [!IMPORTANT]
>
> `.NET 8` now brings better support for adding resilience to `HttpClient`. See [Add resilience to an HTTP client](https://learn.microsoft.com/en-us/dotnet/core/resilience/http-resilience?tabs=dotnet-cli#add-resilience-to-an-http-client) and [Building resilient cloud services with .NET 8 | .NET Conf 2023](https://www.youtube.com/watch?v=BDZpuFI8mMM&list=PLdo4fOcmZ0oULyHSPBx-tQzePOYlhvrAU&index=16).
>
> You should consider adopting the new `.NET 8` API instead of using the one presented here.
## Motivation

Every time I use an `HttpClient` I end up repeating the same [Polly](https://github.com/App-vNext/Polly) usage pattern in my projects to add a circuit breaker policy.
Expand All @@ -25,7 +31,7 @@ The `AddCircuitBreakerPolicy` method is an extension method to the `IHttpClientB

This extension will add a [circuit breaker policy](https://github.com/App-vNext/Polly#advanced-circuit-breaker) wrapped with a [circuit breaker checker policy](/docs/polly/circuit-breaker-checker-policy.md) to the `HttpClient`.

> **Note**
> [!NOTE]
>
> the variable `services` in the examples below is of type `IServiceCollection`. On the default template
> for a Web API you can access it via `builder.services`. Example:
Expand Down
Loading

0 comments on commit 69aa315

Please sign in to comment.