Skip to content

Commit

Permalink
Merge pull request #13 from pengweiqhca/develop
Browse files Browse the repository at this point in the history
Support dispose IAsyncDisposable
  • Loading branch information
tillig authored Jul 21, 2022
2 parents 4d4509f + 770c4df commit e39098f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
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

0 comments on commit e39098f

Please sign in to comment.