|
| 1 | +--- |
| 2 | +title: "ASP0028: ## Analyzer to suggest using IPAddress.IPv6Any instead of IPAddress.Any if applicable" |
| 3 | +ms.date: 11/11/2024 |
| 4 | +description: "Learn about analysis rule ASP0028: Consider using IPAddress.IPv6Any instead of IPAddress.Any" |
| 5 | +author: deaglegross |
| 6 | +monikerRange: '>= aspnetcore-10.0' |
| 7 | +ms.author: deaglegross |
| 8 | +uid: diagnostics/asp0028 |
| 9 | +--- |
| 10 | +# ASP0028: Consider using `IPAddress.IPv6Any` instead of `IPAddress.Any` |
| 11 | + |
| 12 | +| | Value | |
| 13 | +| - | - | |
| 14 | +| **Rule ID** | ASP0028 | |
| 15 | +| **Category** | Usage | |
| 16 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 17 | + |
| 18 | +## Cause |
| 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). |
| 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). |
| 23 | + |
| 24 | +This usage will be reported with a diagnostic message: |
| 25 | +```csharp |
| 26 | +.UseKestrel().ConfigureKestrel(options => |
| 27 | +{ |
| 28 | + options.Listen(IPAddress.Any, ...); |
| 29 | +}) |
| 30 | +``` |
| 31 | + |
| 32 | +## Rule description |
| 33 | + |
| 34 | +The recommended way is to setup Kestrel to listen on `IPv6Any`. |
| 35 | + |
| 36 | +## How to fix violations |
| 37 | + |
| 38 | +For the reported code |
| 39 | +```csharp |
| 40 | +.UseKestrel().ConfigureKestrel(options => |
| 41 | +{ |
| 42 | + options.Listen(IPAddress.Any, ...); |
| 43 | +}) |
| 44 | +``` |
| 45 | + |
| 46 | +One can either explicitly change usage to `IPv6Any`: |
| 47 | +```csharp |
| 48 | +.UseKestrel().ConfigureKestrel(options => |
| 49 | +{ |
| 50 | + options.Listen(IPAddress.IPv6Any, ...); |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +or use another invocation - `options.ListenAnyIP()` without specifying any argument explicitly: |
| 55 | +```csharp |
| 56 | +.UseKestrel().ConfigureKestrel(options => |
| 57 | +{ |
| 58 | + options.ListenAnyIP(...); |
| 59 | +}) |
| 60 | +``` |
| 61 | + |
| 62 | +## When to suppress warnings |
| 63 | + |
| 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. |
| 65 | + |
| 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) |
0 commit comments