如何在 ApiController 上实现 DisposeAsync?

How do you implement DisposeAsync on an ApiController?

我有以下代码可以在 grpc 服务上放置 json api。释放服务激活器句柄时返回 ValueTask。代码检查表明应该等待此代码,但是当我重构为使用 DisposeAsync 模式时 MapControllers 抛出异常(“DisposeAsync 没有属性路由”)。

[ApiController]
public class GatewayController : Controller, IAsyncDisposable
{
    private readonly GrpcService _grpcService;
    private readonly IGrpcServiceActivator<GrpcService> _serviceActivator;
    private readonly GrpcActivatorHandle<GrpcService> _activatorHandle;

    public GatewayController(
        IGrpcServiceActivator<GrpcService> serviceActivator, 
        IServiceProvider serviceProvider)
    {
        _serviceActivator = serviceActivator;
        _activatorHandle = serviceActivator.Create(serviceProvider);
        _grpcService = _activatorHandle.Instance;
    }

    public async ValueTask DisposeAsync()
    {
        await DisposeAsyncCore();
        Dispose(false);
        GC.SuppressFinalize(this);
    }

    protected virtual async ValueTask DisposeAsyncCore()
    {
        // returns a ValueTask that is warned as "should be awaited"
        await _serviceActivator.ReleaseAsync(_activatorHandle);
    }
}

如何在 ApiController 上实施 DisposeAsync

使用NonActionAttribute:

Indicates that a controller method is not an action method.

[NonAction]
public async ValueTask DisposeAsync()
{
   ...
}

explicit interface implementation:

async ValueTask IAsyncDisposable.DisposeAsync()
{
   ...
}