Skip to content
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

Support dispose IAsyncDisposable #13

Merged
merged 3 commits into from
Jul 21, 2022
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
24 changes: 21 additions & 3 deletions src/Autofac.Integration.Web/ContainerDisposalModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Threading.Tasks;
using System.Web;

namespace Autofac.Integration.Web
Expand Down Expand Up @@ -55,18 +56,35 @@ public void Init(HttpApplication context)
if (_containerProviderAccessor == null)
throw new InvalidOperationException(ContainerDisposalModuleResources.ApplicationMustImplementAccessor);

context.EndRequest += OnEndRequest;
var wrapper = new EventHandlerTaskAsyncHelper(OnEndRequest);

context.AddOnEndRequestAsync(wrapper.BeginEventHandler, wrapper.EndEventHandler);
}

/// <summary>
/// Dispose of the per-request container.
/// </summary>
private void OnEndRequest(object sender, EventArgs e)
private Task OnEndRequest(object sender, EventArgs e)
{
var cp = _containerProviderAccessor.ContainerProvider;
if (cp == null)
throw new InvalidOperationException(ContainerDisposalModuleResources.ContainerProviderNull);
cp.EndRequestLifetime();

var valueTask = cp.EndRequestLifetime();

// https://github.com/dotnet/aspnetcore/blob/main/src/Shared/ValueTaskExtensions/ValueTaskExtensions.cs#L13

// Try to avoid the allocation from AsTask
if (valueTask.IsCompletedSuccessfully)
{
// Signal consumption to the IValueTaskSource
valueTask.GetAwaiter().GetResult();
return Task.CompletedTask;
}
else
{
return valueTask.AsTask();
}
}
}
}
6 changes: 3 additions & 3 deletions src/Autofac.Integration.Web/ContainerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Threading.Tasks;
using System.Web;
using Autofac.Core.Lifetime;

Expand Down Expand Up @@ -61,11 +62,10 @@ public ContainerProvider(IContainer applicationContainer, Action<ContainerBuilde
/// Dispose of the current request's container, if it has been
/// instantiated.
/// </summary>
public void EndRequestLifetime()
public ValueTask EndRequestLifetime()
{
var rc = AmbientRequestLifetime;
if (rc != null)
rc.Dispose();
return rc == null ? default : rc.DisposeAsync();
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Autofac.Integration.Web/IContainerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System.Threading.Tasks;

namespace Autofac.Integration.Web
{
/// <summary>
Expand All @@ -34,7 +36,7 @@ public interface IContainerProvider
/// Dispose of the current request's container, if it has been
/// instantiated.
/// </summary>
void EndRequestLifetime();
ValueTask EndRequestLifetime();

/// <summary>
/// Gets the global, application-wide container.
Expand Down