Merge pull request #2528 from mehmetuken/application-shutdown

Application shutdown
pull/2573/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit 2d8b349a27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<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<IAbpApplicationWithExternalServiceProvider>().Initialize(app.ApplicationServices);
application.Initialize(app.ApplicationServices);
}
public static IApplicationBuilder UseAuditing(this IApplicationBuilder app)
@ -42,7 +56,8 @@ namespace Microsoft.AspNetCore.Builder
.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
.GetRequiredService<IAbpRequestLocalizationOptionsProvider>()
@ -62,4 +77,4 @@ namespace Microsoft.AspNetCore.Builder
return app.UseMiddleware<AbpExceptionHandlingMiddleware>();
}
}
}
}

@ -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
/// </summary>
/// <seealso cref="System.IServiceProvider" />
/// <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>
/// Initializes a new instance of the <see cref="AutofacServiceProvider"/> class.
/// </summary>
/// <param name="componentContext">
/// The component context from which services should be resolved.
/// <param name="lifetimeScope">
/// The lifetime scope from which services will be resolved.
/// </param>
public AutofacServiceProvider(IComponentContext componentContext)
public AutofacServiceProvider(ILifetimeScope lifetimeScope)
{
this._componentContext = componentContext;
this._lifetimeScope = lifetimeScope;
}
/// <summary>
@ -66,7 +68,7 @@ namespace Autofac.Extensions.DependencyInjection
/// </exception>
public object GetRequiredService(Type serviceType)
{
return this._componentContext.Resolve(serviceType);
return this._lifetimeScope.Resolve(serviceType);
}
/// <summary>
@ -81,7 +83,40 @@ namespace Autofac.Extensions.DependencyInjection
/// </returns>
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);
}
}
}

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

Loading…
Cancel
Save