Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IServiceScope Tests / Fixes #176

Merged
merged 2 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[assembly: InternalsVisibleTo("Shiny.Prism.Tests")]
namespace Prism.Microsoft.DependencyInjection
{
public class PrismContainerExtension : IContainerExtension<IServiceProvider>, IServiceCollectionAware, IServiceProvider, IContainerInfo
public class PrismContainerExtension : IContainerExtension<IServiceProvider>, IServiceCollectionAware, IServiceProvider, IContainerInfo, IServiceScopeFactory
{
public static IContainerExtension Current
{
Expand Down Expand Up @@ -111,10 +111,11 @@ public PrismContainerExtension(IServiceCollection services)
NamedServiceRegistry = new NamedServiceRegistry();

Services.AddSingleton<PrismContainerExtension>(this);
Services.AddSingleton<IContainerRegistry>(sp => sp.GetService<PrismContainerExtension>());
Services.AddSingleton<IContainerExtension>(sp => sp.GetService<PrismContainerExtension>());
Services.AddSingleton<IContainerProvider>(sp => sp.GetService<PrismContainerExtension>());
Services.AddSingleton<IServiceProvider>(sp => sp.GetService<PrismContainerExtension>());
Services.AddSingleton<IContainerRegistry>(sp => sp.GetRequiredService<PrismContainerExtension>());
Services.AddSingleton<IContainerExtension>(sp => sp.GetRequiredService<PrismContainerExtension>());
Services.AddSingleton<IContainerProvider>(sp => sp.GetRequiredService<PrismContainerExtension>());
Services.AddSingleton<IServiceProvider>(sp => sp.GetRequiredService<PrismContainerExtension>());
Services.AddSingleton<IServiceScopeFactory>(sp => sp.GetRequiredService<PrismContainerExtension>());
}

public void SetServiceCollection(IServiceCollection services)
Expand Down Expand Up @@ -345,12 +346,17 @@ public object GetService(Type serviceType) =>

public IScopedProvider CreateScope()
{

_parentScope = Instance.CreateScope();
_serviceScope = new ConcreteAwareServiceScope(_parentScope);
return new ScopedProvider(_serviceScope, NamedServiceRegistry, Services);
}

IServiceScope IServiceScopeFactory.CreateScope()
{
var scopedProvider = CreateScope() as ScopedProvider;
return scopedProvider.ServiceScope;
}

// TODO: Refactor to share this with the child scope
private IServiceProvider GetChildProvider((Type Type, object Instance)[] parameters)
{
Expand Down Expand Up @@ -397,13 +403,12 @@ Type IContainerInfo.GetRegistrationType(Type serviceType)

private class ScopedProvider : IScopedProvider
{
private IServiceScope _serviceScope;
private NamedServiceRegistry _namedServiceRegistry;
private IServiceCollection _services;

public ScopedProvider(IServiceScope serviceScope, NamedServiceRegistry namedServiceRegistry, IServiceCollection services)
{
_serviceScope = serviceScope;
ServiceScope = serviceScope;
_namedServiceRegistry = namedServiceRegistry;
_services = services;
}
Expand All @@ -412,21 +417,22 @@ public ScopedProvider(IServiceScope serviceScope, NamedServiceRegistry namedServ

public IScopedProvider CurrentScope { get; }

public IServiceScope ServiceScope { get; private set; }

public IScopedProvider CreateScope()
{
throw new NotImplementedException();
return Current.CreateScope();
}

public void Dispose()
{
if (_serviceScope != null)
if (ServiceScope != null)
{
_serviceScope.Dispose();
_serviceScope = null;
ServiceScope.Dispose();
ServiceScope = null;
}
}


public object Resolve(Type type) =>
Resolve(type, Array.Empty<(Type, object)>());

Expand All @@ -437,7 +443,7 @@ public object Resolve(Type type, params (Type Type, object Instance)[] parameter
{
try
{
var provider = parameters.Length > 0 ? GetChildProvider(parameters) : _serviceScope.ServiceProvider;
var provider = parameters.Length > 0 ? GetChildProvider(parameters) : ServiceScope.ServiceProvider;
return provider.GetService(type) ?? throw NullResolutionException(type);
}
catch (Exception ex)
Expand All @@ -450,7 +456,7 @@ public object Resolve(Type type, string name, params (Type Type, object Instance
{
try
{
var provider = parameters.Length > 0 ? GetChildProvider(parameters) : _serviceScope.ServiceProvider;
var provider = parameters.Length > 0 ? GetChildProvider(parameters) : ServiceScope.ServiceProvider;
return _namedServiceRegistry.GetService(provider, name, type) ?? throw NullResolutionException(type, name);
}
catch (Exception ex)
Expand All @@ -467,7 +473,7 @@ private static Exception NullResolutionException(Type type, string name = null)
private IServiceProvider GetChildProvider((Type Type, object Instance)[] parameters)
{
if (parameters is null || parameters.Length == 0)
return _serviceScope.ServiceProvider;
return ServiceScope.ServiceProvider;

var services = new ServiceCollection();
foreach (var service in _services)
Expand All @@ -485,7 +491,7 @@ private IServiceProvider GetChildProvider((Type Type, object Instance)[] paramet

var rootSP = services.BuildServiceProvider();

if (_serviceScope is null)
if (ServiceScope is null)
{
return new ConcreteAwareServiceProvider(rootSP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ public void RegisterServicesExtensionsAddsServicesToContainer()
Assert.True(((IContainerProvider)PrismContainerExtension.Current).IsRegistered<IHttpClientFactory>());
}

[Fact]
public void IServiceScopeFactoryIsRegistered()
{
PrismContainerExtension.Current.RegisterServices(s =>
{
s.AddHttpClient();
});

Assert.True(((IContainerProvider)PrismContainerExtension.Current).IsRegistered<IServiceScopeFactory>());
}

[Fact]
public void ResolvesHttpClientWithHttpClientFactory()
{
PrismContainerExtension.Current.RegisterServices(s =>
{
s.AddHttpClient();
});

HttpClient client = null;
var ex = Record.Exception(() => client = PrismContainerExtension.Current.Resolve<HttpClient>());

Assert.Null(ex);
Assert.NotNull(client);
}

private void ConfigureServices()
{
var services = new ServiceCollection();
Expand Down