|
| 1 | +--- |
| 2 | +title: EXTOBS0001 warning |
| 3 | +description: Learn about the obsoletions that generate compile-time warning EXTOBS0001. |
| 4 | +ms.date: 11/12/2025 |
| 5 | +f1_keywords: |
| 6 | + - extobs0001 |
| 7 | +ai-usage: ai-assisted |
| 8 | +--- |
| 9 | +# EXTOBS0001: IResourceMonitor is obsolete |
| 10 | + |
| 11 | +The <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitor?displayProperty=fullName> interface and related APIs are obsolete starting in .NET 9. These APIs will be removed in a future version. The resource monitoring functionality has been replaced with a more efficient metrics-based approach using observable instruments. |
| 12 | + |
| 13 | +The following APIs are marked obsolete. Use of these APIs generates warning `EXTOBS0001` at compile time. |
| 14 | + |
| 15 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitor> |
| 16 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceMonitoringOptions.CollectionWindow?displayProperty=nameWithType> |
| 17 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceMonitoringOptions.SamplingInterval?displayProperty=nameWithType> |
| 18 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceMonitoringBuilderExtensions> |
| 19 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceUtilization> |
| 20 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitorBuilder> |
| 21 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceUtilizationPublisher> |
| 22 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.ISnapshotProvider> |
| 23 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.Snapshot> |
| 24 | +- <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.SystemResources> |
| 25 | +- <xref:Microsoft.Extensions.DependencyInjection.ResourceMonitoringServiceCollectionExtensions.AddResourceMonitoring(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitorBuilder})?displayProperty=nameWithType> |
| 26 | + |
| 27 | +## Workarounds |
| 28 | + |
| 29 | +Instead of using `IResourceMonitor`, switch to using resource monitoring metrics with observable instruments. The metrics-based approach provides the same resource utilization information (CPU, memory) but integrates better with modern observability systems like OpenTelemetry. |
| 30 | + |
| 31 | +### Migration example |
| 32 | + |
| 33 | +Old approach using `IResourceMonitor`: |
| 34 | + |
| 35 | +```csharp |
| 36 | +services.AddResourceMonitoring(); |
| 37 | + |
| 38 | +// Inject and use IResourceMonitor |
| 39 | +public class MyService |
| 40 | +{ |
| 41 | + private readonly IResourceMonitor _resourceMonitor; |
| 42 | + |
| 43 | + public MyService(IResourceMonitor resourceMonitor) |
| 44 | + { |
| 45 | + _resourceMonitor = resourceMonitor; |
| 46 | + } |
| 47 | + |
| 48 | + public void CheckResources() |
| 49 | + { |
| 50 | + var utilization = _resourceMonitor.GetUtilization(TimeSpan.FromSeconds(1)); |
| 51 | + Console.WriteLine($"CPU: {utilization.CpuUsedPercentage}%"); |
| 52 | + Console.WriteLine($"Memory: {utilization.MemoryUsedPercentage}%"); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +New approach using metrics: |
| 58 | + |
| 59 | +```csharp |
| 60 | +services.AddResourceMonitoring(); |
| 61 | + |
| 62 | +// Configure metrics collection. |
| 63 | +services.AddOpenTelemetry() |
| 64 | + .WithMetrics(builder => |
| 65 | + { |
| 66 | + builder.AddMeter("Microsoft.Extensions.Diagnostics.ResourceMonitoring"); |
| 67 | + builder.AddConsoleExporter(); |
| 68 | + }); |
| 69 | +``` |
| 70 | + |
| 71 | +The resource monitoring metrics are automatically published and can be consumed by any OpenTelemetry-compatible metrics pipeline. For more information, see [`Microsoft.Extensions.Diagnostics.ResourceMonitoring` metrics](../../core/diagnostics/built-in-metrics-diagnostics.md#microsoftextensionsdiagnosticsresourcemonitoring). |
| 72 | + |
| 73 | +## Suppress a warning |
| 74 | + |
| 75 | +If you must use the obsolete APIs, you can suppress the warning in code or in your project file. |
| 76 | + |
| 77 | +To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning. |
| 78 | + |
| 79 | +```csharp |
| 80 | +// Disable the warning. |
| 81 | +#pragma warning disable EXTOBS0001 |
| 82 | + |
| 83 | +// Code that uses obsolete API. |
| 84 | +// ... |
| 85 | +
|
| 86 | +// Re-enable the warning. |
| 87 | +#pragma warning restore EXTOBS0001 |
| 88 | +``` |
| 89 | + |
| 90 | +To suppress all the `EXTOBS0001` warnings in your project, add a `<NoWarn>` property to your project file. |
| 91 | + |
| 92 | +```xml |
| 93 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 94 | + <PropertyGroup> |
| 95 | + ... |
| 96 | + <NoWarn>$(NoWarn);EXTOBS0001</NoWarn> |
| 97 | + </PropertyGroup> |
| 98 | +</Project> |
| 99 | +``` |
| 100 | + |
| 101 | +For more information, see [Suppress warnings](obsoletions-overview.md#suppress-warnings). |
| 102 | + |
| 103 | +## See also |
| 104 | + |
| 105 | +- [Diagnostic resource monitoring](../../core/diagnostics/diagnostic-resource-monitoring.md) |
| 106 | +- [`Microsoft.Extensions.Diagnostics.ResourceMonitoring` metrics](../../core/diagnostics/built-in-metrics-diagnostics.md#microsoftextensionsdiagnosticsresourcemonitoring) |
0 commit comments