mirror of https://github.com/abpframework/abp.git
16 changed files with 25 additions and 171 deletions
@ -1,52 +0,0 @@ |
|||
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() |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -1,22 +0,0 @@ |
|||
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(); |
|||
} |
|||
} |
|||
@ -1,8 +0,0 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.DependencyInjection; |
|||
|
|||
public interface IHybridServiceScopeFactory : IServiceScopeFactory |
|||
{ |
|||
|
|||
} |
|||
@ -1,71 +0,0 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.DependencyInjection; |
|||
|
|||
public class HybridServiceScopeFactory_Tests |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Use_Default_ServiceScopeFactory_By_Default_Async() |
|||
{ |
|||
using (var application = await AbpApplicationFactory.CreateAsync<IndependentEmptyModule>()) |
|||
{ |
|||
application.Services.AddType(typeof(MyServiceAsync)); |
|||
|
|||
await application.InitializeAsync(); |
|||
|
|||
var serviceScopeFactory = application.ServiceProvider.GetRequiredService<IHybridServiceScopeFactory>(); |
|||
|
|||
using (var scope = serviceScopeFactory.CreateScope()) |
|||
{ |
|||
scope.ServiceProvider.GetRequiredService<MyServiceAsync>(); |
|||
} |
|||
|
|||
MyServiceAsync.DisposeCount.ShouldBe(1); |
|||
} |
|||
} |
|||
|
|||
[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 MyServiceAsync : ITransientDependency, IDisposable |
|||
{ |
|||
public static int DisposeCount { get; private set; } |
|||
|
|||
public void Dispose() |
|||
{ |
|||
++DisposeCount; |
|||
} |
|||
} |
|||
|
|||
private class MyService : ITransientDependency, IDisposable |
|||
{ |
|||
public static int DisposeCount { get; private set; } |
|||
|
|||
public void Dispose() |
|||
{ |
|||
++DisposeCount; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue