-
-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Func<TArgs, Task> handlers for Core Autofac Events
- Loading branch information
Showing
9 changed files
with
300 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Autofac Project. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Autofac.Util | ||
{ | ||
/// <summary> | ||
/// Adapts an async action to the <see cref="IAsyncDisposable"/> interface. | ||
/// </summary> | ||
internal class AsyncReleaseAction<TLimit> : Disposable | ||
{ | ||
private readonly Func<TLimit, ValueTask> _action; | ||
private readonly Func<TLimit> _factory; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncReleaseAction{TLimit}"/> class. | ||
/// </summary> | ||
/// <param name="action"> | ||
/// The async action to execute on disposal. | ||
/// </param> | ||
/// <param name="factory"> | ||
/// A factory that retrieves the value on which the <paramref name="action" /> | ||
/// should be executed. | ||
/// </param> | ||
public AsyncReleaseAction(Func<TLimit, ValueTask> action, Func<TLimit> factory) | ||
{ | ||
if (action == null) throw new ArgumentNullException(nameof(action)); | ||
if (factory == null) throw new ArgumentNullException(nameof(factory)); | ||
|
||
_action = action; | ||
_factory = factory; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override async ValueTask DisposeAsync(bool disposing) | ||
{ | ||
// Value retrieval for the disposal is deferred until | ||
// disposal runs to ensure any calls to, say, .ReplaceInstance() | ||
// during .OnActivating() will be accounted for. | ||
if (disposing) | ||
{ | ||
await _action(_factory()).ConfigureAwait(false); | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
// Value retrieval for the disposal is deferred until | ||
// disposal runs to ensure any calls to, say, .ReplaceInstance() | ||
// during .OnActivating() will be accounted for. | ||
if (disposing) | ||
{ | ||
var vt = _action(_factory()); | ||
|
||
if (!vt.IsCompletedSuccessfully) | ||
{ | ||
vt.ConfigureAwait(false).GetAwaiter().GetResult(); | ||
} | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
} | ||
} |
Oops, something went wrong.