Skip to content

Commit

Permalink
Merge pull request #200 from seesharper/implement-iasyncdisposable
Browse files Browse the repository at this point in the history
Implement IAsyncDisposable in LightInjectServiceScope
  • Loading branch information
seesharper authored Oct 11, 2022
2 parents 83bd28e + 0ce8648 commit 79ae9f5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"type": "shell",
"options": {
"cwd": "src"
"cwd": "${workspaceFolder}"
},
"group": "build",
"presentation": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace LightInject.Microsoft.DependencyInjection.Tests
{
public class AsyncDisposableTests
{
[Fact]
public async Task ShouldDisposeAsyncDisposable()
{
var serviceCollection = new ServiceCollection();
List<object> disposedObjects = new();
serviceCollection.AddScoped<AsyncDisposable>(sp => new AsyncDisposable(disposedObject => disposedObjects.Add(disposedObject)));

var serviceProvider = serviceCollection.CreateLightInjectServiceProvider();

AsyncDisposable asyncDisposable = null;
await using (var scope = serviceProvider.CreateAsyncScope())
{
asyncDisposable = scope.ServiceProvider.GetService<AsyncDisposable>();
}

Assert.Contains(asyncDisposable, disposedObjects);
}
}

public class AsyncDisposable : IAsyncDisposable
{
private readonly Action<object> onDisposed;

public AsyncDisposable(Action<object> onDisposed)
{
this.onDisposed = onDisposed;
}
public ValueTask DisposeAsync()
{
onDisposed(this);
return ValueTask.CompletedTask;
}
}

public class Disposable : IDisposable
{
private readonly Action<object> onDisposed;

public Disposable(Action<object> onDisposed)
{
this.onDisposed = onDisposed;
}

public void Dispose()
{
onDisposed(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace LightInject.Microsoft.DependencyInjection
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using global::Microsoft.Extensions.DependencyInjection;

/// <summary>
Expand Down Expand Up @@ -387,7 +388,7 @@ public IServiceScope CreateScope()
/// <summary>
/// An <see cref="IServiceScope"/> implementation that wraps a <see cref="Scope"/>.
/// </summary>
internal class LightInjectServiceScope : IServiceScope
internal class LightInjectServiceScope : IServiceScope, IAsyncDisposable
{
private readonly Scope wrappedScope;

Expand All @@ -403,10 +404,11 @@ public LightInjectServiceScope(Scope scope)

public IServiceProvider ServiceProvider { get; }

/// <summary>
/// Disposes the wrapped <see cref="Scope"/>.
/// </summary>
/// <inheritdoc/>
public void Dispose() => wrappedScope.Dispose();

/// <inheritdoc/>
public ValueTask DisposeAsync() => wrappedScope.DisposeAsync();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<Authors>Bernhard Richter</Authors>
<Description>Enables LightInject to be used as the service container in ASP.NET Core and Entity Framework 7 applications.</Description>
<Copyright>Bernhard Richter</Copyright>
Expand All @@ -26,4 +26,11 @@
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<Choose>
<When Condition=" '$(TargetFramework)'=='netstandard2.0'">
<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
</ItemGroup>
</When>
</Choose>
</Project>

0 comments on commit 79ae9f5

Please sign in to comment.