Skip to content

API Proposal: InputFile.Dispose(bool) virtual dispose pattern #66386

@javiercn

Description

@javiercn

Background and Motivation

InputFile previously used explicit interface implementation for IDisposable.Dispose(), making it difficult for derived classes to properly dispose resources without reflection. A user implementing a derived class for handling cancel events (see #58118) had to use reflection to dispose _jsCallbacksRelay. The standard .NET dispose pattern with protected virtual void Dispose(bool) enables derived classes to extend disposal behavior cleanly.

Source: PR #64771, fixes #64392 (also references #58118).

Proposed API

namespace Microsoft.AspNetCore.Components.Forms;

public class InputFile : ComponentBase, IDisposable
{
+    protected virtual void Dispose(bool disposing);
}

Usage Examples

public class CustomInputFile : InputFile
{
    private SomeResource _resource;

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _resource?.Dispose();
        }
        base.Dispose(disposing);
    }
}

Alternative Designs

Keep explicit IDisposable.Dispose() and require reflection for derived classes. This was rejected as it violates the .NET dispose pattern guidelines.

Risks

Technically a behavioral change — existing derived classes that shadow Dispose() may behave differently. However, this follows established .NET patterns (InputBase, ValidationMessage already use this pattern).

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedapi-proposalarea-blazorIncludes: Blazor, Razor Components

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions