Skip to content

[Breaking change]: Generated WMI wrappers implement IDisposable #55779

Description

@jeffhandley

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.

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.

// 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.

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.
  • 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.
  • 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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions