You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -598,6 +598,29 @@ var container = new ServiceContainer(o => o.EnableCurrentScope = false);
598
598
599
599
This also improves performance ever so slightly as we don't need to maintain a current scope when scopes are started and ended.
600
600
601
+
### IAsyncDisposable
602
+
603
+
LightInject also supports [IAsyncDisposable](https://docs.microsoft.com/en-us/dotnet/api/system.iasyncdisposable) meaning that [IAsyncDisposable.DisposeAsync](https://docs.microsoft.com/en-us/dotnet/api/system.iasyncdisposable.disposeasync) will be called if the scope is started with a using-block adding the await `await` keyword.
The `Scope` returned from `BeginScope` also implements `IAsyncDisposable` and will call `DisposeAsync` on all scoped services resolved within the `Scope`.
613
+
Services only implementing `IDisposable` will also be disposed the the async scope ends.
614
+
615
+
If on the other hand, a service ONLY implements `IAsyncDisposable` and is resolved within a synchronous scope, an exception will be thrown
@@ -6657,14 +6658,14 @@ public Scope(ServiceContainer serviceFactory)
6657
6658
/// <summary>
6658
6659
/// Registers the <paramref name="disposable"/> so that it is disposed when the scope is completed.
6659
6660
/// </summary>
6660
-
/// <param name="disposable">The <see cref="IDisposable"/> object to register.</param>
6661
-
publicvoidTrackInstance(IDisposabledisposable)
6661
+
/// <param name="disposable">The <see cref="IDisposable"/> or <see cref="IAsyncDisposable"/> object to register.</param>
6662
+
publicvoidTrackInstance(objectdisposable)
6662
6663
{
6663
6664
lock(lockObject)
6664
6665
{
6665
6666
if(disposableObjects==null)
6666
6667
{
6667
-
disposableObjects=newList<IDisposable>();
6668
+
disposableObjects=newList<object>();
6668
6669
}
6669
6670
6670
6671
disposableObjects.Add(disposable);
@@ -6689,6 +6690,10 @@ public void Dispose()
6689
6690
disposable.Dispose();
6690
6691
}
6691
6692
}
6693
+
else
6694
+
{
6695
+
thrownewInvalidOperationException($"The type {disposableObjects[i].GetType()} only implements `IAsyncDisposable` and can only be disposed in an asynchronous scope started with `BeginScopeAsync()`");
0 commit comments