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
title: "ASP0028: ## Analyzer to suggest using IPAddress.IPv6Any instead of IPAddress.Any if applicable"
3
3
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"
5
5
author: deaglegross
6
6
monikerRange: '>= aspnetcore-10.0'
7
-
ms.author: deaglegross
7
+
ms.author: dmkorolev
8
8
uid: diagnostics/asp0028
9
9
---
10
10
# ASP0028: Consider using `IPAddress.IPv6Any` instead of `IPAddress.Any`
@@ -17,11 +17,17 @@ uid: diagnostics/asp0028
17
17
18
18
## Cause
19
19
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).
21
21
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:
23
30
24
-
This usage will be reported with a diagnostic message:
25
31
```csharp
26
32
.UseKestrel().ConfigureKestrel(options=>
27
33
{
@@ -31,36 +37,34 @@ This usage will be reported with a diagnostic message:
31
37
32
38
## Rule description
33
39
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`.
35
41
36
42
## How to fix violations
37
43
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`.
45
45
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
48
49
.UseKestrel().ConfigureKestrel(options =>
49
50
{
50
-
options.Listen(IPAddress.IPv6Any, ...);
51
+
- options.Listen(IPAddress.Any, ...);
52
+
+ options.ListenAnyIP(...);
51
53
})
52
54
```
53
55
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
56
59
.UseKestrel().ConfigureKestrel(options =>
57
60
{
58
-
options.ListenAnyIP(...);
61
+
- options.Listen(IPAddress.Any, ...);
62
+
+ options.Listen(IPAddress.IPv6Any, ...);
59
63
})
60
64
```
61
65
62
66
## When to suppress warnings
63
67
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.
65
69
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)
0 commit comments