Introduced IHybridServiceScopeFactory

pull/625/head
Halil ibrahim Kalkan 7 years ago
parent 8714c3488a
commit 71cdde8a3c

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;

@ -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
{
}
}

@ -9,7 +9,7 @@ namespace Volo.Abp.EventBus
/// <remarks>
/// This class always creates a new transient instance of handler.
/// </remarks>
internal class TransientEventHandlerFactory<THandler> : IEventHandlerFactory
public class TransientEventHandlerFactory<THandler> : IEventHandlerFactory
where THandler : IEventHandler, new()
{
/// <summary>

@ -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…
Cancel
Save