Skip to content

Commit

Permalink
Fixed components resolved from typed factories being disposed along w…
Browse files Browse the repository at this point in the history
…ith unrelated objects
  • Loading branch information
jnm2 committed Oct 12, 2018
1 parent f35e627 commit 73a7a7f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Breaking Changes:
- Removal of deprecated member LifestyleHandlerType on CustomLifestyleAttribute (@fir3pho3nixx, #338)
- Removed Event Wiring, Factory Support and Synchronize facilities (@jonorossi, #403)

Bugfixes:
- Fixed components resolved from typed factories being disposed along with unrelated objects (@jnm2, #439)

## 4.1.0 (2017-09-28)

Bugfixes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ public virtual object Resolve(IKernelInternal kernel, IReleasePolicy scope)
throw;
}
}
return kernel.Resolve(componentType, additionalArguments, scope);

// Ignore thread-static parent context call stack tracking. Factory-resolved components
// are already tracked by the factory itself and should not be added as burdens just because
// we happen to be resolving in the call stack of some random component’s constructor.

// Specifically, act the same as we would if the timing was slightly different and we were not
// resolving within the call stack of the random component’s constructor.
return kernel.Resolve(componentType, additionalArguments, scope, ignoreParentContext: true);
}

private bool LoadByName(IKernelInternal kernel)
Expand Down
9 changes: 7 additions & 2 deletions src/Castle.Windsor/MicroKernel/DefaultKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,17 @@ protected virtual void RegisterSubSystems()
}

protected object ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
{
return ResolveComponent(handler, service, additionalArguments, policy, ignoreParentContext: false);
}

private object ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy, bool ignoreParentContext)
{
Debug.Assert(handler != null, "handler != null");
var parent = currentCreationContext;
var context = CreateCreationContext(handler, service, additionalArguments, parent, policy);
currentCreationContext = context;
var context = CreateCreationContext(handler, service, additionalArguments, ignoreParentContext ? null : parent, policy);

currentCreationContext = context;
try
{
return handler.Resolve(context);
Expand Down
4 changes: 2 additions & 2 deletions src/Castle.Windsor/MicroKernel/DefaultKernel_Resolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ object IKernelInternal.Resolve(String key, Type service, IDictionary arguments,
return ResolveComponent(handler, service ?? typeof(object), arguments, policy);
}

object IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
object IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy, bool ignoreParentContext)
{
var handler = (this as IKernelInternal).LoadHandlerByType(null, service, arguments);
if(handler == null)
{
throw new ComponentNotFoundException(service);
}
return ResolveComponent(handler, service, arguments, policy);
return ResolveComponent(handler, service, arguments, policy, ignoreParentContext);
}

Array IKernelInternal.ResolveAll(Type service, IDictionary arguments, IReleasePolicy policy)
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Windsor/MicroKernel/IKernelInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface IKernelInternal : IKernel

IDisposable OptimizeDependencyResolution();

object Resolve(Type service, IDictionary arguments, IReleasePolicy policy);
object Resolve(Type service, IDictionary arguments, IReleasePolicy policy, bool ignoreParentContext = false);

/// <summary>
/// Returns a component instance by the key
Expand Down

0 comments on commit 73a7a7f

Please sign in to comment.