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

Make sure IServiceScope do not get disposed from the scope if there is no recreation. #148

Merged
merged 2 commits into from
Sep 12, 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 @@ -5,31 +5,19 @@ namespace Prism.Microsoft.DependencyInjection
{
public class ConcreteAwareServiceScope : IServiceScope
{
private IServiceScope _serviceScope;

public ConcreteAwareServiceScope(IServiceScope serviceScope)
{
_serviceScope = serviceScope;
ServiceProvider = new ConcreteAwareServiceProvider(_serviceScope.ServiceProvider); ;
ServiceProvider = new ConcreteAwareServiceProvider(serviceScope.ServiceProvider);
}

public IServiceProvider ServiceProvider { get; }

#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls


protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_serviceScope.Dispose();
_serviceScope = null;
}

disposedValue = true;
}
}

// This code added to correctly implement the disposable pattern.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ private static IContainerExtension TryGetContainer()
private NamedServiceRegistry NamedServiceRegistry { get; }
public IServiceCollection Services { get; private set; }


private IServiceScope _parentScope=null;
private IServiceProvider _instance;
public IServiceProvider Instance
{
Expand All @@ -81,15 +83,15 @@ public IServiceProvider Instance
if (_serviceScope != null)
{
createScope = true;
_serviceScope.Dispose();
_parentScope?.Dispose();
}

_instance = new ConcreteAwareServiceProvider(Services.BuildServiceProvider());
requiresRebuild = false;

if(createScope)
{
_serviceScope = new ConcreteAwareServiceScope(_instance.CreateScope());
_parentScope = _instance.CreateScope();
_serviceScope = new ConcreteAwareServiceScope(_parentScope);
}
}

Expand Down Expand Up @@ -343,7 +345,9 @@ public object GetService(Type serviceType) =>

public IScopedProvider CreateScope()
{
_serviceScope = new ConcreteAwareServiceScope(Instance.CreateScope());

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

Expand Down Expand Up @@ -422,6 +426,7 @@ public void Dispose()
}
}


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

Expand Down