@@ -1079,7 +1079,7 @@ internal static void TryThrowUnableToResolve(Request request)
1079
1079
str = request.Container
1080
1080
.GetAllServiceFactories(request.ServiceType, bothClosedAndOpenGenerics: true)
1081
1081
.Aggregate(str, (s, x) => s
1082
- .Append(x.Value.Reuse?.CanApply(request) ?? true ? " " : " without matching scope ")
1082
+ .Append(( x.Value.Reuse?.CanApply(request) ?? true) ? " " : " without matching scope ")
1083
1083
.Print(x));
1084
1084
1085
1085
if (str.Length != 0)
@@ -1171,8 +1171,9 @@ Factory IContainer.GetServiceFactoryOrDefault(Request request)
1171
1171
var factories = entry == null ? null
1172
1172
: entry is Factory factory ? new KV<object, Factory>(DefaultKey.Value, factory).One()
1173
1173
: entry.To<FactoriesEntry>().Factories
1174
- .Visit(new List<KV<object, Factory>>(2), (x, list) => list.Add(KV.Of(x.Key, x.Value))).ToArray(); // todo: optimize - we may not need ToArray here
1175
-
1174
+ .Visit(new List<KV<object, Factory>>(2), (x, list) => list.Add(KV.Of(x.Key, x.Value))).ToArray() // todo: optimize - we may not need ToArray here
1175
+ .Match(x => x.Value != null); // filter out the Unregistered factories (see #390)
1176
+
1176
1177
if (!factories.IsNullOrEmpty()) // check for the additional (not the fallback) factories, because we have the standard factories
1177
1178
{
1178
1179
if (Rules.HasDynamicRegistrationProvider(
@@ -1456,9 +1457,10 @@ IEnumerable<KV<object, Factory>> IContainer.GetAllServiceFactories(Type serviceT
1456
1457
private static IEnumerable<KV<object, Factory>> GetRegistryEntryKeyFactoryPairs(object entry) =>
1457
1458
entry == null
1458
1459
? Empty<KV<object, Factory>>()
1459
- : entry is Factory ? new[] { new KV<object, Factory>(DefaultKey.Value, (Factory)entry ) }
1460
+ : entry is Factory f ? new[] { new KV<object, Factory>(DefaultKey.Value, f ) }
1460
1461
// todo: optimize
1461
- : entry.To<FactoriesEntry>().Factories.Visit(new List<KV<object, Factory>>(), (x, l) => l.Add(KV.Of(x.Key, x.Value))).ToArray();
1462
+ : entry.To<FactoriesEntry>().Factories.Visit(new List<KV<object, Factory>>(), (x, l) => l.Add(KV.Of(x.Key, x.Value))).ToArray()
1463
+ .Match(x => x.Value != null); // filter out the Unregistered factories
1462
1464
1463
1465
Expression IContainer.GetDecoratorExpressionOrDefault(Request request)
1464
1466
{
@@ -2709,19 +2711,23 @@ private Registry UnregisterServiceFactory(Type serviceType, object serviceKey =
2709
2711
2710
2712
var registry = WithServices(services);
2711
2713
2712
- var removedFactory = removed as Factory;
2713
- if (removedFactory != null)
2714
- registry.DropFactoryCache(removedFactory, hash, serviceType, serviceKey);
2714
+ if (removed is Factory f)
2715
+ registry.DropFactoryCache(f, hash, serviceType, serviceKey);
2716
+ else if (removed is Factory[] fs)
2717
+ foreach (var rf in fs)
2718
+ registry.DropFactoryCache(rf, hash, serviceType, serviceKey);
2715
2719
else
2716
- (removed as Factory[] ??
2717
- ((FactoriesEntry)removed).Factories.Enumerate().Select(f => f.Value).ToArray())
2718
- .ForEach(x => registry.DropFactoryCache(x, hash, serviceType, serviceKey));
2720
+ foreach (var e in ((FactoriesEntry)removed).Factories.Enumerate())
2721
+ registry.DropFactoryCache(e.Value, hash, serviceType, serviceKey);
2719
2722
2720
2723
return registry;
2721
2724
}
2722
2725
2723
2726
internal void DropFactoryCache(Factory factory, int hash, Type serviceType, object serviceKey = null)
2724
2727
{
2728
+ if (factory == null)
2729
+ return; // filter out Unregistered factory (see #390)
2730
+
2725
2731
if (DefaultFactoryCache != null || KeyedFactoryCache != null)
2726
2732
{
2727
2733
if (factory.FactoryGenerator == null)
0 commit comments