-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#23 - Child container now uses a scope from the parent
- Loading branch information
Showing
6 changed files
with
185 additions
and
63 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
103 changes: 103 additions & 0 deletions
103
src/DependencyInjection.ReRouting/DisposableServiceProvider.cs
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,103 @@ | ||
namespace Dazinator.Extensions.DependencyInjection | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Decorates an inner service provider, adapting it to implement <see cref="IDisposable.Dispose"/> | ||
/// and <see cref="IAsyncDisposable.DisposeAsync"/> - so that additional provided callbacks can be fired on disposal. | ||
/// </summary> | ||
public class DisposableServiceProvider : IServiceProvider, IDisposable | ||
#if SUPPORTS_ASYNC_DISPOSE | ||
, IAsyncDisposable | ||
#endif | ||
{ | ||
private IServiceProvider _inner; | ||
private Action _onDispose; | ||
|
||
#if SUPPORTS_ASYNC_DISPOSE | ||
private Func<Task> _onAsyncDispose; | ||
#endif | ||
|
||
public DisposableServiceProvider(IServiceProvider inner, | ||
Action onDispose = null | ||
#if SUPPORTS_ASYNC_DISPOSE | ||
, Func<Task> onAsyncDispose = null | ||
#endif | ||
) | ||
{ | ||
_inner = inner; | ||
_onDispose = onDispose; | ||
#if SUPPORTS_ASYNC_DISPOSE | ||
_onAsyncDispose = onAsyncDispose; | ||
#endif | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public object GetService(Type serviceType) => _inner.GetService(serviceType); | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
// Dispose of inner provider first. | ||
if (_inner is IDisposable innerDisposable) | ||
{ | ||
innerDisposable.Dispose(); | ||
} | ||
|
||
_onDispose?.Invoke(); | ||
} | ||
_inner = null; | ||
_onDispose = null; | ||
#if SUPPORTS_ASYNC_DISPOSE | ||
_onAsyncDispose = null; | ||
#endif | ||
} | ||
|
||
#if SUPPORTS_ASYNC_DISPOSE | ||
public async ValueTask DisposeAsync() | ||
{ | ||
await DisposeAsyncCore(); | ||
|
||
Dispose(disposing: false); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual async ValueTask DisposeAsyncCore() | ||
{ | ||
|
||
if (_inner is IAsyncDisposable innerDisposableAsync) | ||
{ | ||
await innerDisposableAsync.DisposeAsync(); | ||
} | ||
else if (_inner is IDisposable innerDisposable) | ||
{ | ||
innerDisposable.Dispose(); | ||
} | ||
|
||
if (_onAsyncDispose != null) | ||
{ | ||
await _onAsyncDispose().ConfigureAwait(false); | ||
} | ||
|
||
//if (_onDispose != null) | ||
//{ | ||
// _onDispose?.Invoke(); | ||
//} | ||
|
||
_onAsyncDispose = null; | ||
_onDispose = null; | ||
_inner = null; | ||
} | ||
#endif | ||
|
||
} | ||
|
||
|
||
} |
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,32 @@ | ||
namespace DependencyInjection.ReRouting | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
public static class DisposeHelper | ||
{ | ||
|
||
#if SUPPORTS_ASYNC_DISPOSE | ||
public static async Task DisposeAsyncIfImplemented(object objectToBeDisposed) | ||
{ | ||
if (objectToBeDisposed is IAsyncDisposable asyncDisposable) | ||
{ | ||
await asyncDisposable.DisposeAsync(); | ||
} | ||
else | ||
{ | ||
DisposeIfImplemented(objectToBeDisposed); | ||
} | ||
} | ||
|
||
#endif | ||
|
||
public static void DisposeIfImplemented(object objectToBeDisposed) | ||
{ | ||
if (objectToBeDisposed is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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