Browse Source

Merge pull request #2528 from mehmetuken/application-shutdown

Application shutdown
pull/2573/head
Halil İbrahim Kalkan 7 years ago
committed by GitHub
parent
commit
2d8b349a27
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs
  2. 53
      framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacServiceProvider.cs
  3. 10
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs

21
framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs

@ -2,6 +2,7 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.AspNetCore.RequestLocalization; using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.ExceptionHandling;
@ -19,8 +20,21 @@ namespace Microsoft.AspNetCore.Builder
{ {
Check.NotNull(app, nameof(app)); Check.NotNull(app, nameof(app));
var application = app.ApplicationServices.GetRequiredService<IAbpApplicationWithExternalServiceProvider>();
var applicationLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
applicationLifetime.ApplicationStopping.Register(() =>
{
application.Shutdown();
});
applicationLifetime.ApplicationStopped.Register(() =>
{
application.Dispose();
});
app.ApplicationServices.GetRequiredService<ObjectAccessor<IApplicationBuilder>>().Value = app; app.ApplicationServices.GetRequiredService<ObjectAccessor<IApplicationBuilder>>().Value = app;
app.ApplicationServices.GetRequiredService<IAbpApplicationWithExternalServiceProvider>().Initialize(app.ApplicationServices); application.Initialize(app.ApplicationServices);
} }
public static IApplicationBuilder UseAuditing(this IApplicationBuilder app) public static IApplicationBuilder UseAuditing(this IApplicationBuilder app)
@ -42,7 +56,8 @@ namespace Microsoft.AspNetCore.Builder
.UseMiddleware<AbpCorrelationIdMiddleware>(); .UseMiddleware<AbpCorrelationIdMiddleware>();
} }
public static IApplicationBuilder UseAbpRequestLocalization(this IApplicationBuilder app, Action<RequestLocalizationOptions> optionsAction = null) public static IApplicationBuilder UseAbpRequestLocalization(this IApplicationBuilder app,
Action<RequestLocalizationOptions> optionsAction = null)
{ {
app.ApplicationServices app.ApplicationServices
.GetRequiredService<IAbpRequestLocalizationOptionsProvider>() .GetRequiredService<IAbpRequestLocalizationOptionsProvider>()
@ -62,4 +77,4 @@ namespace Microsoft.AspNetCore.Builder
return app.UseMiddleware<AbpExceptionHandlingMiddleware>(); return app.UseMiddleware<AbpExceptionHandlingMiddleware>();
} }
} }
} }

53
framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacServiceProvider.cs

@ -1,6 +1,6 @@
// This software is part of the Autofac IoC container // This software is part of the Autofac IoC container
// Copyright © 2015 Autofac Contributors // Copyright © 2015 Autofac Contributors
// http://autofac.org // https://autofac.org
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -33,19 +33,21 @@ namespace Autofac.Extensions.DependencyInjection
/// </summary> /// </summary>
/// <seealso cref="System.IServiceProvider" /> /// <seealso cref="System.IServiceProvider" />
/// <seealso cref="Microsoft.Extensions.DependencyInjection.ISupportRequiredService" /> /// <seealso cref="Microsoft.Extensions.DependencyInjection.ISupportRequiredService" />
public class AutofacServiceProvider : IServiceProvider, ISupportRequiredService public class AutofacServiceProvider : IServiceProvider, ISupportRequiredService, IDisposable
{ {
private readonly IComponentContext _componentContext; private readonly ILifetimeScope _lifetimeScope;
private bool _disposed = false;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AutofacServiceProvider"/> class. /// Initializes a new instance of the <see cref="AutofacServiceProvider"/> class.
/// </summary> /// </summary>
/// <param name="componentContext"> /// <param name="lifetimeScope">
/// The component context from which services should be resolved. /// The lifetime scope from which services will be resolved.
/// </param> /// </param>
public AutofacServiceProvider(IComponentContext componentContext) public AutofacServiceProvider(ILifetimeScope lifetimeScope)
{ {
this._componentContext = componentContext; this._lifetimeScope = lifetimeScope;
} }
/// <summary> /// <summary>
@ -66,7 +68,7 @@ namespace Autofac.Extensions.DependencyInjection
/// </exception> /// </exception>
public object GetRequiredService(Type serviceType) public object GetRequiredService(Type serviceType)
{ {
return this._componentContext.Resolve(serviceType); return this._lifetimeScope.Resolve(serviceType);
} }
/// <summary> /// <summary>
@ -81,7 +83,40 @@ namespace Autofac.Extensions.DependencyInjection
/// </returns> /// </returns>
public object GetService(Type serviceType) public object GetService(Type serviceType)
{ {
return this._componentContext.ResolveOptional(serviceType); return this._lifetimeScope.ResolveOptional(serviceType);
}
/// <summary>
/// Gets the underlying instance of <see cref="ILifetimeScope" />.
/// </summary>
public ILifetimeScope LifetimeScope => _lifetimeScope;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true" /> to release both managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
this._disposed = true;
if (disposing)
{
this._lifetimeScope.Dispose();
}
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} }
} }
} }

10
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationWithExternalServiceProvider.cs

@ -26,5 +26,15 @@ namespace Volo.Abp
InitializeModules(); InitializeModules();
} }
public override void Dispose()
{
base.Dispose();
if (ServiceProvider is IDisposable disposableServiceProvider)
{
disposableServiceProvider.Dispose();
}
}
} }
} }

Loading…
Cancel
Save