From 908f28297a5bb61e5e6662bbcb4b094280965184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Tu=CC=88ken?= Date: Thu, 2 Jan 2020 02:31:47 +0300 Subject: [PATCH 1/3] Implement IDisposable on AutofacServiceProvider. --- .../AutofacServiceProvider.cs | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacServiceProvider.cs b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacServiceProvider.cs index ff2d45b2a4..1c1a24ff66 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacServiceProvider.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacServiceProvider.cs @@ -1,6 +1,6 @@ // This software is part of the Autofac IoC container // Copyright © 2015 Autofac Contributors -// http://autofac.org +// https://autofac.org // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -33,19 +33,21 @@ namespace Autofac.Extensions.DependencyInjection /// /// /// - public class AutofacServiceProvider : IServiceProvider, ISupportRequiredService + public class AutofacServiceProvider : IServiceProvider, ISupportRequiredService, IDisposable { - private readonly IComponentContext _componentContext; + private readonly ILifetimeScope _lifetimeScope; + + private bool _disposed = false; /// /// Initializes a new instance of the class. /// - /// - /// The component context from which services should be resolved. + /// + /// The lifetime scope from which services will be resolved. /// - public AutofacServiceProvider(IComponentContext componentContext) + public AutofacServiceProvider(ILifetimeScope lifetimeScope) { - this._componentContext = componentContext; + this._lifetimeScope = lifetimeScope; } /// @@ -66,7 +68,7 @@ namespace Autofac.Extensions.DependencyInjection /// public object GetRequiredService(Type serviceType) { - return this._componentContext.Resolve(serviceType); + return this._lifetimeScope.Resolve(serviceType); } /// @@ -81,7 +83,40 @@ namespace Autofac.Extensions.DependencyInjection /// public object GetService(Type serviceType) { - return this._componentContext.ResolveOptional(serviceType); + return this._lifetimeScope.ResolveOptional(serviceType); + } + + /// + /// Gets the underlying instance of . + /// + public ILifetimeScope LifetimeScope => _lifetimeScope; + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// + /// to release both managed and unmanaged resources; + /// to release only unmanaged resources. + /// + protected virtual void Dispose(bool disposing) + { + if (!this._disposed) + { + this._disposed = true; + if (disposing) + { + this._lifetimeScope.Dispose(); + } + } + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); } } } \ No newline at end of file From bf1960213fb5aa319454a095226ba1ec82018369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Tu=CC=88ken?= Date: Thu, 2 Jan 2020 02:33:52 +0300 Subject: [PATCH 2/3] Listen IHostApplicationLifetime Stopping and Stopped event. --- .../AbpApplicationBuilderExtensions.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index 22c0079571..033fef813f 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -2,6 +2,7 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.RequestLocalization; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Volo.Abp; using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.AspNetCore.ExceptionHandling; @@ -19,8 +20,21 @@ namespace Microsoft.AspNetCore.Builder { Check.NotNull(app, nameof(app)); + var application = app.ApplicationServices.GetRequiredService(); + var applicationLifetime = app.ApplicationServices.GetRequiredService(); + + applicationLifetime.ApplicationStopping.Register(() => + { + application.Shutdown(); + }); + + applicationLifetime.ApplicationStopped.Register(() => + { + application.Dispose(); + }); + app.ApplicationServices.GetRequiredService>().Value = app; - app.ApplicationServices.GetRequiredService().Initialize(app.ApplicationServices); + application.Initialize(app.ApplicationServices); } public static IApplicationBuilder UseAuditing(this IApplicationBuilder app) @@ -42,7 +56,8 @@ namespace Microsoft.AspNetCore.Builder .UseMiddleware(); } - public static IApplicationBuilder UseAbpRequestLocalization(this IApplicationBuilder app, Action optionsAction = null) + public static IApplicationBuilder UseAbpRequestLocalization(this IApplicationBuilder app, + Action optionsAction = null) { app.ApplicationServices .GetRequiredService() @@ -62,4 +77,4 @@ namespace Microsoft.AspNetCore.Builder return app.UseMiddleware(); } } -} +} \ No newline at end of file From 8d5d0adb83f261b9e51188146638ea2b4339325d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Tu=CC=88ken?= Date: Thu, 2 Jan 2020 02:35:33 +0300 Subject: [PATCH 3/3] Disposing AbpApplicationWithExternalServiceProvider. --- .../Abp/AbpApplicationWithExternalServiceProvider.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs index 1becd3be9b..58f75f3b96 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs @@ -26,5 +26,15 @@ namespace Volo.Abp InitializeModules(); } + + public override void Dispose() + { + base.Dispose(); + + if (ServiceProvider is IDisposable disposableServiceProvider) + { + disposableServiceProvider.Dispose(); + } + } } }