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

Intermittent “No Default Instance defined for PluginFamily” exceptions #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/OpenRasta.DI.StructureMap/StructureMapDependencyResolver.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRasta.Diagnostics;
using StructureMap;
using StructureMap.Pipeline;

namespace OpenRasta.DI.StructureMap
{
public class StructureMapDependencyResolver : DependencyResolverCore, IDependencyResolver
{
private readonly IContainer _container;
private readonly object _locker = new object();

public StructureMapDependencyResolver()
: this(ObjectFactory.Container)
Expand All @@ -17,6 +20,7 @@ public StructureMapDependencyResolver()
public StructureMapDependencyResolver(IContainer container)
{
_container = container;
_container.Configure(ex => ex.FillAllPropertiesOfType<ILogger>());
}

protected override void AddDependencyCore(Type serviceType, Type concreteType, DependencyLifetime lifetime)
Expand Down Expand Up @@ -46,22 +50,34 @@ protected override void AddDependencyCore(Type concreteType, DependencyLifetime

protected override void AddDependencyInstanceCore(Type serviceType, object instance, DependencyLifetime lifetime)
{
_container.Configure(cfg => cfg.For(serviceType).LifecycleIs(GetLifecycle(lifetime)).Use(instance));
lock (_locker)
{
_container.Configure(cfg => cfg.For(serviceType).LifecycleIs(GetLifecycle(lifetime)).Use(instance));
}
}

protected override IEnumerable<TService> ResolveAllCore<TService>()
{
return _container.GetAllInstances<TService>();
lock (_locker)
{
return _container.GetAllInstances<TService>();
}
}

protected override object ResolveCore(Type serviceType)
{
return _container.GetInstance(serviceType);
lock (_locker)
{
return _container.GetInstance(serviceType);
}
}

public bool HasDependency(Type serviceType)
{
return _container.TryGetInstance(serviceType) != null;
lock (_locker)
{
return _container.TryGetInstance(serviceType) != null;
}
}

public bool HasDependencyImplementation(Type serviceType, Type concreteType)
Expand All @@ -74,7 +90,7 @@ public bool HasDependencyImplementation(Type serviceType, Type concreteType)

public void HandleIncomingRequestProcessed()
{
// meh
HttpContextLifecycle.DisposeAndClearAll();
}
}
}