Skip to content

[Mvc] Support IAsyncDisposable for controllers and pages #29313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Mvc/Mvc.Core/src/Controllers/ControllerActivatorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -14,8 +15,11 @@ namespace Microsoft.AspNetCore.Mvc.Controllers
public class ControllerActivatorProvider : IControllerActivatorProvider
{
private static readonly Action<ControllerContext, object> _dispose = Dispose;
private static readonly Func<ControllerContext, object, ValueTask> _disposeAsync = DisposeAsync;
private static readonly Func<ControllerContext, object, ValueTask> _syncDisposeAsync = SyncDisposeAsync;
private readonly Func<ControllerContext, object> _controllerActivatorCreate;
private readonly Action<ControllerContext, object> _controllerActivatorRelease;
private readonly Func<ControllerContext, object, ValueTask> _controllerActivatorReleaseAsync;

/// <summary>
/// Initializes a new instance of <see cref="ControllerActivatorProvider"/>.
Expand All @@ -33,6 +37,7 @@ public ControllerActivatorProvider(IControllerActivator controllerActivator)
{
_controllerActivatorCreate = controllerActivator.Create;
_controllerActivatorRelease = controllerActivator.Release;
_controllerActivatorReleaseAsync = controllerActivator.ReleaseAsync;
}
}

Expand Down Expand Up @@ -83,6 +88,32 @@ public Action<ControllerContext, object> CreateReleaser(ControllerActionDescript
return null;
}

/// <inheritdoc/>
public Func<ControllerContext, object, ValueTask> CreateAsyncReleaser(ControllerActionDescriptor descriptor)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}

if (_controllerActivatorReleaseAsync != null)
{
return _controllerActivatorReleaseAsync;
}

if (typeof(IAsyncDisposable).GetTypeInfo().IsAssignableFrom(descriptor.ControllerTypeInfo))
{
return _disposeAsync;
}

if (typeof(IDisposable).GetTypeInfo().IsAssignableFrom(descriptor.ControllerTypeInfo))
{
return _syncDisposeAsync;
}

return null;
}

private static void Dispose(ControllerContext context, object controller)
{
if (controller == null)
Expand All @@ -92,5 +123,21 @@ private static void Dispose(ControllerContext context, object controller)

((IDisposable)controller).Dispose();
}

private static ValueTask DisposeAsync(ControllerContext context, object controller)
{
if (controller == null)
{
throw new ArgumentNullException(nameof(controller));
}

return ((IAsyncDisposable)controller).DisposeAsync();
}

private static ValueTask SyncDisposeAsync(ControllerContext context, object controller)
{
Dispose(context, controller);
return default;
}
}
}
29 changes: 28 additions & 1 deletion src/Mvc/Mvc.Core/src/Controllers/ControllerFactoryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Core;

namespace Microsoft.AspNetCore.Mvc.Controllers
Expand All @@ -13,6 +14,7 @@ internal class ControllerFactoryProvider : IControllerFactoryProvider
private readonly IControllerActivatorProvider _activatorProvider;
private readonly Func<ControllerContext, object> _factoryCreateController;
private readonly Action<ControllerContext, object> _factoryReleaseController;
private readonly Func<ControllerContext, object, ValueTask> _factoryReleaseControllerAsync;
private readonly IControllerPropertyActivator[] _propertyActivators;

public ControllerFactoryProvider(
Expand All @@ -37,6 +39,7 @@ public ControllerFactoryProvider(
{
_factoryCreateController = controllerFactory.CreateController;
_factoryReleaseController = controllerFactory.ReleaseController;
_factoryReleaseControllerAsync = controllerFactory.ReleaseControllerAsync;
}

_propertyActivators = propertyActivators.ToArray();
Expand Down Expand Up @@ -104,6 +107,30 @@ public Action<ControllerContext, object> CreateControllerReleaser(ControllerActi
return _activatorProvider.CreateReleaser(descriptor);
}

public Func<ControllerContext, object, ValueTask> CreateAsyncControllerReleaser(ControllerActionDescriptor descriptor)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}

var controllerType = descriptor.ControllerTypeInfo?.AsType();
if (controllerType == null)
{
throw new ArgumentException(Resources.FormatPropertyOfTypeCannotBeNull(
nameof(descriptor.ControllerTypeInfo),
nameof(descriptor)),
nameof(descriptor));
}

if (_factoryReleaseControllerAsync != null)
{
return _factoryReleaseControllerAsync;
}

return _activatorProvider.CreateAsyncReleaser(descriptor);
}

private Action<ControllerContext, object>[] GetPropertiesToActivate(ControllerActionDescriptor actionDescriptor)
{
var propertyActivators = new Action<ControllerContext, object>[_propertyActivators.Length];
Expand Down
22 changes: 22 additions & 0 deletions src/Mvc/Mvc.Core/src/Controllers/DefaultControllerActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Infrastructure;

Expand Down Expand Up @@ -74,5 +75,26 @@ public void Release(ControllerContext context, object controller)
disposable.Dispose();
}
}

public ValueTask ReleaseAsync(ControllerContext context, object controller)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (controller == null)
{
throw new ArgumentNullException(nameof(controller));
}

if (controller is IAsyncDisposable asyncDisposable)
{
return asyncDisposable.DisposeAsync();
}

Release(context, controller);
return default;
}
}
}
16 changes: 16 additions & 0 deletions src/Mvc/Mvc.Core/src/Controllers/DefaultControllerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Core;

namespace Microsoft.AspNetCore.Mvc.Controllers
Expand Down Expand Up @@ -83,5 +84,20 @@ public void ReleaseController(ControllerContext context, object controller)

_controllerActivator.Release(context, controller);
}

public ValueTask ReleaseControllerAsync(ControllerContext context, object controller)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (controller == null)
{
throw new ArgumentNullException(nameof(controller));
}

return _controllerActivator.ReleaseAsync(context, controller);
}
}
}
15 changes: 14 additions & 1 deletion src/Mvc/Mvc.Core/src/Controllers/IControllerActivator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Mvc.Controllers
{
/// <summary>
Expand All @@ -20,5 +22,16 @@ public interface IControllerActivator
/// <param name="context">The <see cref="ControllerContext"/> for the executing action.</param>
/// <param name="controller">The controller to release.</param>
void Release(ControllerContext context, object controller);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete and never delete.


/// <summary>
/// Releases a controller asynchronously.
/// </summary>
/// <param name="context">The <see cref="ControllerContext"/> for the executing action.</param>
/// <param name="controller">The controller to release.</param>
ValueTask ReleaseAsync(ControllerContext context, object controller)
{
Release(context, controller);
return default;
}
}
}
}
18 changes: 17 additions & 1 deletion src/Mvc/Mvc.Core/src/Controllers/IControllerActivatorProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Mvc.Controllers
{
Expand All @@ -23,5 +24,20 @@ public interface IControllerActivatorProvider
/// <param name="descriptor">The <see cref="ControllerActionDescriptor"/>.</param>
/// <returns>The delegate used to dispose the activated controller.</returns>
Action<ControllerContext, object> CreateReleaser(ControllerActionDescriptor descriptor);

/// <summary>
/// Creates an <see cref="Action"/> that releases a controller.
/// </summary>
/// <param name="descriptor">The <see cref="ControllerActionDescriptor"/>.</param>
/// <returns>The delegate used to dispose the activated controller.</returns>
Func<ControllerContext, object, ValueTask> CreateAsyncReleaser(ControllerActionDescriptor descriptor)
{
var releaser = CreateReleaser(descriptor);
return (context, controller) =>
{
releaser(context, controller);
return default;
};
}
}
}
13 changes: 13 additions & 0 deletions src/Mvc/Mvc.Core/src/Controllers/IControllerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Mvc.Controllers
{
/// <summary>
Expand All @@ -21,5 +23,16 @@ public interface IControllerFactory
/// <param name="context"><see cref="ControllerContext"/> for the executing action.</param>
/// <param name="controller">The controller.</param>
void ReleaseController(ControllerContext context, object controller);

/// <summary>
/// Releases a controller instance asynchronously.
/// </summary>
/// <param name="context"><see cref="ControllerContext"/> for the executing action.</param>
/// <param name="controller">The controller.</param>
ValueTask ReleaseControllerAsync(ControllerContext context, object controller)
{
ReleaseController(context, controller);
return default;
}
}
}
18 changes: 17 additions & 1 deletion src/Mvc/Mvc.Core/src/Controllers/IControllerFactoryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Mvc.Controllers
{
Expand All @@ -23,5 +24,20 @@ public interface IControllerFactoryProvider
/// <param name="descriptor">The <see cref="ControllerActionDescriptor"/>.</param>
/// <returns>The delegate used to release the created controller.</returns>
Action<ControllerContext, object> CreateControllerReleaser(ControllerActionDescriptor descriptor);

/// <summary>
/// Releases a controller asynchronously.
/// </summary>
/// <param name="descriptor">The <see cref="ControllerActionDescriptor"/>.</param>
/// <returns>The delegate used to release the created controller asynchronously.</returns>
Func<ControllerContext, object, ValueTask> CreateAsyncControllerReleaser(ControllerActionDescriptor descriptor)
{
var releaser = CreateControllerReleaser(descriptor);
return (context, controller) =>
{
releaser(context, controller);
return default;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ internal ControllerActionInvoker(
// Internal for testing
internal ControllerContext ControllerContext => _controllerContext;

protected override void ReleaseResources()
protected override ValueTask ReleaseResources()
{
if (_instance != null && _cacheEntry.ControllerReleaser != null)
{
_cacheEntry.ControllerReleaser(_controllerContext, _instance);
return _cacheEntry.ControllerReleaser(_controllerContext, _instance);
}

return default;
}

private Task Next(ref State next, ref Scope scope, ref object? state, ref bool isCompleted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ControllerActionInvokerCache(
parameterDefaultValues);

var controllerFactory = _controllerFactoryProvider.CreateControllerFactory(actionDescriptor);
var controllerReleaser = _controllerFactoryProvider.CreateControllerReleaser(actionDescriptor);
var controllerReleaser = _controllerFactoryProvider.CreateAsyncControllerReleaser(actionDescriptor);
var propertyBinderFactory = ControllerBinderDelegateProvider.CreateBinderDelegate(
_parameterBinder,
_modelBinderFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable enable

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Internal;
Expand All @@ -15,7 +16,7 @@ internal class ControllerActionInvokerCacheEntry
internal ControllerActionInvokerCacheEntry(
FilterItem[] cachedFilters,
Func<ControllerContext, object> controllerFactory,
Action<ControllerContext, object> controllerReleaser,
Func<ControllerContext, object, ValueTask> controllerReleaser,
ControllerBinderDelegate controllerBinderDelegate,
ObjectMethodExecutor objectMethodExecutor,
ActionMethodExecutor actionMethodExecutor)
Expand All @@ -32,7 +33,7 @@ internal ControllerActionInvokerCacheEntry(

public Func<ControllerContext, object> ControllerFactory { get; }

public Action<ControllerContext, object> ControllerReleaser { get; }
public Func<ControllerContext, object, ValueTask> ControllerReleaser { get; }

public ControllerBinderDelegate ControllerBinderDelegate { get; }

Expand Down
Loading