dotnet / dotnet/aspnetcore

Handling of `Disposable` arguments and responses in controller actions

Open
#49,023 3 comments 6 reactions 0 assignees View on GitHub
area-mvc Needs: Attention :wave:
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

In my project I would like to allow use of `JsonDocument` type in controllers actions. So end developers could expose API that returns and excepts raw Json as root or nested object.

`JsonDocuiment` is properly serialized using `System.Text.Json` however it is a disposable object and ASP controllers don`t handle dispose of them when response is serialized.

As a library author I'm aware I could ask developers to use `httpContext.Response.RegisterForDispose` to handle disposal of returned objects and manually handle disposal of arguments passed to controller actions as well.

However this is so uncommon I doubt developers will remember this and therefore I will risk invalid implementations where disposal will be handled by GC.

Is there a reason why Controllers do not handle this case them self ?

### Describe the solution you'd like

Perhaps you could detect those types on bootstrap level when API Explorer is doing its magic. You could cache than metadata about required disposal and using that cache hook arguments/responses disposal when request handling is done.

### Additional context

_No response_

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.