From aa0beec183ecd764e577b48b02f6cd49ce508e7b Mon Sep 17 00:00:00 2001
From: Peter Csajtai <peter.csajtai@outlook.com>
Date: Tue, 6 Dec 2022 16:54:43 +0100
Subject: [PATCH] Fix name comparison during named scope resolution

---
 CHANGELOG.md                                     | 3 +++
 src/Lifetime/NamedScopeLifetime.cs               | 2 +-
 src/Registration/SelectionRules/ScopeNameRule.cs | 2 +-
 test/NamedScopeTests.cs                          | 5 +++--
 4 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b8b1c83..09f5e9fb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 - `WhenResolutionPathHas()` & `WhenInResolutionPathOf()` registration options for handling more conditional resolution cases. They extend the original *parent type* and *attribute* conditions with inheritance.
 
+### Fixed
+- Name comparison during named scope resolution.
+
 ## [v5.5.3] - 2022-11-29
 ### Fixed
 - `IsRegistered()` produced falsy results on requests with dynamically constructed string service names.
diff --git a/src/Lifetime/NamedScopeLifetime.cs b/src/Lifetime/NamedScopeLifetime.cs
index ecfa412d..aa6ad35d 100644
--- a/src/Lifetime/NamedScopeLifetime.cs
+++ b/src/Lifetime/NamedScopeLifetime.cs
@@ -45,7 +45,7 @@ private static object GetScopedValue(IResolutionScope currentScope, IRequestCont
             Func<IResolutionScope, IRequestContext, object> factory, Type serviceType, int scopeId, object scopeName)
         {
             var scope = currentScope;
-            while (scope != null && scope.Name != scopeName)
+            while (scope != null && !scopeName.Equals(scope.Name))
                 scope = scope.ParentScope;
 
             if (scope == null)
diff --git a/src/Registration/SelectionRules/ScopeNameRule.cs b/src/Registration/SelectionRules/ScopeNameRule.cs
index c01ebfae..9ee715c2 100644
--- a/src/Registration/SelectionRules/ScopeNameRule.cs
+++ b/src/Registration/SelectionRules/ScopeNameRule.cs
@@ -18,7 +18,7 @@ public bool IsValidForCurrentRequest(TypeInformation typeInformation,
             if (resolutionContext.ScopeNames.Length == 0)
                 return false;
 
-            shouldIncrementWeight = resolutionContext.ScopeNames.First() == namedScopeLifetime.ScopeName;
+            shouldIncrementWeight = resolutionContext.ScopeNames.First().Equals(namedScopeLifetime.ScopeName);
             return resolutionContext.ScopeNames.Contains(namedScopeLifetime.ScopeName);
         }
     }
diff --git a/test/NamedScopeTests.cs b/test/NamedScopeTests.cs
index 6977c615..b55a2753 100644
--- a/test/NamedScopeTests.cs
+++ b/test/NamedScopeTests.cs
@@ -16,11 +16,12 @@ public class NamedScopeTests
         [ClassData(typeof(CompilerTypeTestData))]
         public void NamedScope_Simple_Resolve_Prefer_Named(CompilerType compilerType)
         {
+            var name = "A".ToLower();
             var inst = new StashboxContainer(config => config.WithCompiler(compilerType))
                 .Register<ITest, Test11>()
-                .Register<ITest, Test>(config => config.InNamedScope("A"))
+                .Register<ITest, Test>(config => config.InNamedScope(name))
                 .Register<ITest, Test1>()
-                .BeginScope("A")
+                .BeginScope(name)
                 .Resolve<ITest>();
 
             Assert.IsType<Test>(inst);