mirror of https://github.com/abpframework/abp
parent
8714c3488a
commit
71cdde8a3c
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.AspNetCore.DependencyInjection
|
||||
{
|
||||
[ExposeServices(
|
||||
typeof(IHybridServiceScopeFactory),
|
||||
typeof(HttpContextServiceScopeFactory)
|
||||
)]
|
||||
[Dependency(ReplaceServices = true)]
|
||||
public class HttpContextServiceScopeFactory : IHybridServiceScopeFactory, ITransientDependency
|
||||
{
|
||||
protected IHttpContextAccessor HttpContextAccessor { get; }
|
||||
|
||||
protected IServiceScopeFactory ServiceScopeFactory { get; }
|
||||
|
||||
public HttpContextServiceScopeFactory(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IServiceScopeFactory serviceScopeFactory)
|
||||
{
|
||||
HttpContextAccessor = httpContextAccessor;
|
||||
ServiceScopeFactory = serviceScopeFactory;
|
||||
}
|
||||
|
||||
public virtual IServiceScope CreateScope()
|
||||
{
|
||||
var httpContext = HttpContextAccessor.HttpContext;
|
||||
if (httpContext == null)
|
||||
{
|
||||
return ServiceScopeFactory.CreateScope();
|
||||
}
|
||||
|
||||
return new NonDisposedHttpContextServiceScope(httpContext.RequestServices);
|
||||
}
|
||||
|
||||
protected class NonDisposedHttpContextServiceScope : IServiceScope
|
||||
{
|
||||
public IServiceProvider ServiceProvider { get; }
|
||||
|
||||
public NonDisposedHttpContextServiceScope(IServiceProvider serviceProvider)
|
||||
{
|
||||
ServiceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.DependencyInjection
|
||||
{
|
||||
[ExposeServices(
|
||||
typeof(IHybridServiceScopeFactory),
|
||||
typeof(DefaultServiceScopeFactory)
|
||||
)]
|
||||
public class DefaultServiceScopeFactory : IHybridServiceScopeFactory, ITransientDependency
|
||||
{
|
||||
protected IServiceScopeFactory Factory { get; }
|
||||
|
||||
public DefaultServiceScopeFactory(IServiceScopeFactory factory)
|
||||
{
|
||||
Factory = factory;
|
||||
}
|
||||
|
||||
public IServiceScope CreateScope()
|
||||
{
|
||||
return Factory.CreateScope();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.DependencyInjection
|
||||
{
|
||||
public interface IHybridServiceScopeFactory : IServiceScopeFactory
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue