mirror of https://github.com/abpframework/abp.git
6 changed files with 128 additions and 1 deletions
@ -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 |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.DependencyInjection |
|||
{ |
|||
public class HybridServiceScopeFactory_Tests |
|||
{ |
|||
[Fact] |
|||
public void Should_Use_Default_ServiceScopeFactory_By_Default() |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<IndependentEmptyModule>()) |
|||
{ |
|||
application.Services.AddType(typeof(MyService)); |
|||
|
|||
application.Initialize(); |
|||
|
|||
var serviceScopeFactory = application.ServiceProvider.GetRequiredService<IHybridServiceScopeFactory>(); |
|||
|
|||
using (var scope = serviceScopeFactory.CreateScope()) |
|||
{ |
|||
scope.ServiceProvider.GetRequiredService<MyService>(); |
|||
} |
|||
|
|||
MyService.DisposeCount.ShouldBe(1); |
|||
} |
|||
} |
|||
|
|||
private class MyService : ITransientDependency, IDisposable |
|||
{ |
|||
public static int DisposeCount { get; private set; } |
|||
|
|||
public void Dispose() |
|||
{ |
|||
++DisposeCount; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue