dotnet / dotnet/docs

[Breaking change]: Generated WMI wrappers implement IDisposable

Open
#55,779 0 comments 0 reactions 1 assignee Claimed by @gewarren View on GitHub
:watch: Not Triaged breaking-change
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

## Description

Generated strongly typed WMI collection wrappers and their enumerators now implement `IDisposable`. Calling `Dispose()` releases the wrapped `ManagementObjectCollection` or `ManagementObjectEnumerator` and its COM-backed WMI resources deterministically.

For more information, see [dotnet/runtime#132587](https://github.com/dotnet/runtime/pull/132587).

## Version

.NET 11 RC 2

## Previous behavior

The collection and enumerator types emitted by `ManagementClass.GetStronglyTypedClassCode` did not implement `IDisposable`. A generated collection could not be used in a `using` statement, and consumers could not deterministically release the WMI resources held by its wrapped `ManagementObjectCollection` or `ManagementObjectEnumerator`.

```csharp
// The generated ServiceCollection type did not implement IDisposable.
Service.ServiceCollection services = service.SelectAll();
```

## New behavior

The generated collection and enumerator types implement `IDisposable`. Consumers can dispose them to release the underlying WMI resources.

```csharp
using Service.ServiceCollection services = service.SelectAll();

foreach (Service item in services)
{
// Use item.
}
```

Adding `IDisposable` to these generated, non-sealed types can affect code that derives from a generated wrapper and already declares a `Dispose` member or implements its own disposal pattern.

## Type of breaking change

- [ ] **Binary incompatible**: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- [x] **Source incompatible**: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
- [x] **Behavioral change**: Existing binaries might behave differently at run time.

## Reason for change

The generated wrappers hold COM-backed WMI resources. This change exposes disposal behavior already supported by the wrapped System.Management types so consumers can release those resources deterministically. The existing finalizer fallback of the wrapped types remains unchanged.

## Recommended action

Dispose generated collection and enumerator wrappers when they are no longer needed, preferably with `using` or `using var`.

If your code derives from a generated collection or enumerator wrapper and already defines a disposal pattern, review the generated type's new `IDisposable` implementation. Update the derived type to avoid conflicting `Dispose` members and to preserve disposal of both its own resources and the base wrapper's resources.

## Feature area

Core .NET libraries

## Affected APIs

- `System.Management.ManagementClass.GetStronglyTypedClassCode(bool, bool)` generated collection types
- `System.Management.ManagementClass.GetStronglyTypedClassCode(bool, bool)` generated enumerator types

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.