Skip to content

Commit 0ddfbb8

Browse files
edit asp0028 /1 (#34095)
* edit asp0028 /1 * Update aspnetcore/fundamentals/static-files/samples/9.x/StaticFilesSample/Program.cs * edit asp0028 /1 * edit asp0028 /1 * edit asp0028 /1 * edit asp0028 /1 * Apply suggestions from code review Co-authored-by: Tom Dykstra <[email protected]> * react to feedback * react to feedback * react to feedback * Apply suggestions from code review Co-authored-by: Tom Dykstra <[email protected]> * Update aspnetcore/diagnostics/asp0028.md Co-authored-by: Tom Dykstra <[email protected]> * react to feedback --------- Co-authored-by: Tom Dykstra <[email protected]>
1 parent d7965e3 commit 0ddfbb8

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

aspnetcore/diagnostics/asp0028.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: "ASP0028: ## Analyzer to suggest using IPAddress.IPv6Any instead of IPAddress.Any if applicable"
33
ms.date: 11/11/2024
4-
description: "Learn about analysis rule ASP0028: Consider using IPAddress.IPv6Any instead of IPAddress.Any"
4+
description: "Consider using IPAddress.IPv6Any instead of IPAddress.Any"
55
author: deaglegross
66
monikerRange: '>= aspnetcore-10.0'
7-
ms.author: deaglegross
7+
ms.author: dmkorolev
88
uid: diagnostics/asp0028
99
---
1010
# ASP0028: Consider using `IPAddress.IPv6Any` instead of `IPAddress.Any`
@@ -17,11 +17,17 @@ uid: diagnostics/asp0028
1717

1818
## Cause
1919

20-
On the server machine that supports IPv6, listening to `Any`, rather than `IPv6Any` will either not work or be slower than necessary, because of the [underlying System types implementation](https://github.com/dotnet/runtime/issues/82404).
20+
On the server machine that supports `IPv6`, [IPv6Any](/dotnet/api/system.net.ipaddress.ipv6any) is preferred to [Any](/dotnet/api/system.net.ipaddress.any) because `Any` can be slower than `IPv6Any`. In some cases, `Any` may not work at all. `Any` can be slower due to the [underlying System types implementation](https://github.com/dotnet/runtime/issues/82404).
2121

22-
At the moment of current article publishing, in case of HTTP/1.x or HTTP/2.0 a name like `localhost` will resolve to `[::1]`, which won't be accepted by the server, forcing a retry with `127.0.0.1` (i.e. failed attempt before each connection).
22+
`127.0.0.1` is the IPv4 loopback address. `::1` is the IPv6 loopback address. `Any` is the wildcard address for IPv4. `IPv6Any` is the wildcard address for IPv6.
23+
24+
Current behavior with with IPv6 when using HTTP/1.x or HTTP/2.0:
25+
26+
* `localhost` resolves to `[::1]`.
27+
* `[::1]` isn't accepted by the server, which forces a retry using `127.0.0.1`, creating a repeated cycle.
28+
29+
Using `Any` with the preceding conditions causes the `ASP0028` diagnostic message. Here's an example of the code that can result in these conditions:
2330

24-
This usage will be reported with a diagnostic message:
2531
```csharp
2632
.UseKestrel().ConfigureKestrel(options =>
2733
{
@@ -31,36 +37,34 @@ This usage will be reported with a diagnostic message:
3137

3238
## Rule description
3339

34-
The recommended way is to setup Kestrel to listen on `IPv6Any`.
40+
The recommended way to configure Kestrel to listen for incoming connections on all available `IPv6` network interfaces is with `IPv6Any`.
3541

3642
## How to fix violations
3743

38-
For the reported code
39-
```csharp
40-
.UseKestrel().ConfigureKestrel(options =>
41-
{
42-
options.Listen(IPAddress.Any, ...);
43-
})
44-
```
44+
For the problematic code, replace `Any` with `IPv6Any`.
4545

46-
One can either explicitly change usage to `IPv6Any`:
47-
```csharp
46+
Use the <xref:Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions.ListenAnyIP(System.Int32)> method without specifying any arguments:
47+
48+
```diff
4849
.UseKestrel().ConfigureKestrel(options =>
4950
{
50-
options.Listen(IPAddress.IPv6Any, ...);
51+
- options.Listen(IPAddress.Any, ...);
52+
+ options.ListenAnyIP(...);
5153
})
5254
```
5355

54-
or use another invocation - `options.ListenAnyIP()` without specifying any argument explicitly:
55-
```csharp
56+
Or use the <xref:System.Net.IPAddress.IPv6Any> field:
57+
58+
```diff
5659
.UseKestrel().ConfigureKestrel(options =>
5760
{
58-
options.ListenAnyIP(...);
61+
- options.Listen(IPAddress.Any, ...);
62+
+ options.Listen(IPAddress.IPv6Any, ...);
5963
})
6064
```
6165

6266
## When to suppress warnings
6367

64-
The severity level of this diagnostic is Information. You can suppress warnings if your intention is to disable `IPv6` usage completely on the server.
68+
The `ASP0028` diagnostic has [Information](/dotnet/api/microsoft.extensions.logging.loglevel) level severity. Suppress this warning if your intention is to disable `IPv6` usage completely on the server, although doing so risks the performance problems mentioned in this article.
6569

66-
You can disable IPv6 either system-wide, or for .NET only via the [AppCtx switch or environment variable](https://devblogs.microsoft.com/dotnet/dotnet-6-networking-improvements/#an-option-to-globally-disable-ipv6)
70+
`IPv6` can be disabled either system-wide, or for .NET only via the [AppCtx switch or environment variable](https://devblogs.microsoft.com/dotnet/dotnet-6-networking-improvements/#an-option-to-globally-disable-ipv6)

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,8 @@ items:
13011301
uid: diagnostics/asp0025
13021302
- name: ASP0026
13031303
uid: diagnostics/asp0026
1304+
- name: ASP0028
1305+
uid: diagnostics/asp0028
13041306
- name: BL0001
13051307
uid: diagnostics/bl0001
13061308
- name: BL0002

0 commit comments

Comments
 (0)