Skip to content

Commit 8529c4f

Browse files
committed
Updated nuget test adapter fixed a resolution bug
The bug happened when you register a service with one NON keyed component then again with KEYED components. The adapter incorrectly checked only the first returned service for KEYED and returns null. Identified after update to Orleans 8.1.0
1 parent 36bbbf1 commit 8529c4f

File tree

9 files changed

+49
-32
lines changed

9 files changed

+49
-32
lines changed

src/Castle.Facilities.AspNet.Mvc.Tests/Castle.Facilities.AspNet.Mvc.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.3" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1919
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="11.0.0" />
2020
<PackageReference Include="Microsoft.Web.Infrastructure" Version="1.0.0" />
2121
<PackageReference Include="NUnit" Version="3.13.3" />
22-
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
22+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

src/Castle.Facilities.AspNet.SystemWeb.Tests/Castle.Facilities.AspNet.SystemWeb.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
2323
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="11.0.0" />
2424
<PackageReference Include="NUnit" Version="3.13.3" />
25-
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
25+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

src/Castle.Facilities.AspNet.WebApi.Tests/Castle.Facilities.AspNet.WebApi.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="Microsoft.AspNet.WebApi" Version="5.2.3" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1919
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="11.0.0" />
2020
<PackageReference Include="Microsoft.Web.Infrastructure" Version="1.0.0" />
2121
<PackageReference Include="NUnit" Version="3.13.3" />
22-
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
22+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

src/Castle.Facilities.AspNetCore.Tests/Castle.Facilities.AspNetCore.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<ItemGroup>
88
<!-- This is an intentional upgrade to NUnit. This is the solution for https://github.com/castleproject/Windsor/issues/243 once we upgrade NUnit and make dotnet test a first class citizen-->
99
<PackageReference Include="NUnit" Version="3.13.3" />
10-
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
10+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1212
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
1313
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
1414
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.2" />

src/Castle.Facilities.WcfIntegration.Tests/Castle.Facilities.WcfIntegration.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1616
<PackageReference Include="NUnit" Version="3.13.3" />
17-
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
17+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

src/Castle.Windsor.Extensions.DependencyInjection.Tests/Castle.Windsor.Extensions.DependencyInjection.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1515
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
1616
<PackageReference Include="xunit" Version="2.4.2" />
1717
<PackageReference Include="xunit.assert" Version="2.4.2" />

src/Castle.Windsor.Extensions.DependencyInjection.Tests/CustomAssumptionTests.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#if NET8_0_OR_GREATER
22
using Microsoft.Extensions.DependencyInjection;
3-
using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
43
using System;
54
using System.Linq;
65
using System.Threading;
@@ -110,6 +109,21 @@ public void Scoped_service_resolved_outside_scope()
110109
Assert.Equal(resolvedOutsideScope, resolvedAgainOutsideScope);
111110
}
112111

112+
[Fact]
113+
public void Mix_of_keyed_and_not_keyed()
114+
{
115+
var serviceCollection = GetServiceCollection();
116+
serviceCollection.AddSingleton<ITestService, TestService>();
117+
serviceCollection.AddKeyedSingleton<ITestService, AnotherTestService>("bla");
118+
119+
_serviceProvider = BuildServiceProvider(serviceCollection);
120+
121+
//can resolve the non-keyed
122+
var nonKeyed = _serviceProvider.GetRequiredService<ITestService>();
123+
Assert.NotNull(nonKeyed);
124+
Assert.IsType<TestService>(nonKeyed);
125+
}
126+
113127
[Fact]
114128
public void Scoped_service_resolved_outside_scope_in_another_thread()
115129
{
@@ -167,7 +181,8 @@ public async void Simulate_async_timer_without_wait()
167181
ITestService resolvedInThread = null;
168182
async Task ExecuteAsync()
169183
{
170-
while (!stop)
184+
DateTime start = DateTime.UtcNow;
185+
while (!stop && DateTime.UtcNow.Subtract(start).TotalSeconds < 10)
171186
{
172187
await Task.Delay(100);
173188
if (shouldResolve)
@@ -178,10 +193,7 @@ async Task ExecuteAsync()
178193
}
179194
}
180195
//fire and forget
181-
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
182196
var task = ExecuteAsync();
183-
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
184-
185197
await Task.Delay(500);
186198

187199
var serviceCollection = GetServiceCollection();
@@ -353,7 +365,6 @@ public void TryToResolveScopedInOtherThread()
353365
Assert.True(task.Result);
354366
}
355367

356-
357368
protected override void Dispose(bool disposing)
358369
{
359370
base.Dispose(disposing);
@@ -364,16 +375,10 @@ protected override void Dispose(bool disposing)
364375
}
365376
}
366377

367-
internal class TestService : ITestService
368-
{
369-
}
378+
internal class TestService : ITestService;
370379

371-
internal class AnotherTestService : ITestService
372-
{
373-
}
380+
internal class AnotherTestService : ITestService;
374381

375-
internal interface ITestService
376-
{
377-
}
382+
internal interface ITestService;
378383
}
379384
#endif

src/Castle.Windsor.Extensions.DependencyInjection/WindsorScopedServiceProvider.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Castle.Windsor.Extensions.DependencyInjection
1616
{
17+
using Castle.MicroKernel;
1718
using Castle.MicroKernel.Handlers;
1819
using Castle.Windsor;
1920
using Castle.Windsor.Extensions.DependencyInjection.Scope;
@@ -99,15 +100,26 @@ private object ResolveInstanceOrNull(Type serviceType, bool isOptional)
99100
//this is complicated by the concept of keyed service, because if you are about to resolve WITHOUTH KEY you do not
100101
//need to resolve keyed services. Now Keyed services are available only in version 8 but we register with an helper
101102
//all registered services so we can know if a service was really registered with keyed service or not.
102-
var componentRegistration = container.Kernel.GetHandler(serviceType);
103-
if (componentRegistration.ComponentModel.Name.StartsWith(KeyedRegistrationHelper.KeyedRegistrationPrefix))
103+
var componentRegistrations = container.Kernel.GetHandlers(serviceType);
104+
105+
//now since the caller requested a NON Keyed component, we need to skip all keyed components.
106+
var realRegistrations = componentRegistrations.Where(x => !x.ComponentModel.Name.StartsWith(KeyedRegistrationHelper.KeyedRegistrationPrefix)).ToList();
107+
if (realRegistrations.Count == 0)
104108
{
105-
//Component was registered as keyed component, so we really need to resolve with null because this is the old interface
106-
//so no key is provided.
109+
//No component is registered for the interface without key, resolution cannot be done.
107110
return null;
108111
}
109-
#endif
112+
else if (realRegistrations.Count > 1)
113+
{
114+
//more than one component is registered for the interface without key, resolution cannot be done.
115+
throw new ComponentResolutionException($"More than one component is registered for the interface {serviceType.FullName} without key, resolution cannot be done.");
116+
}
117+
118+
return container.Resolve(realRegistrations[0].ComponentModel.Name, serviceType);
119+
#else
120+
//no keyed component, just resolve.
110121
return container.Resolve(serviceType);
122+
#endif
111123
}
112124

113125
if (serviceType.GetTypeInfo().IsGenericType && serviceType.GetGenericTypeDefinition() == typeof(IEnumerable<>))

src/Castle.Windsor.Tests/Castle.Windsor.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<PackageReference Include="Castle.Core-log4net" Version="[5.1.0,6.0)" />
4848
<PackageReference Include="Castle.Core-NLog" Version="[5.1.0,6.0)" />
4949
<PackageReference Include="NUnit" Version="3.13.3" />
50-
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
50+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
5151
</ItemGroup>
5252

5353
<ItemGroup Condition="'$(TargetFramework)'=='net462'">

0 commit comments

Comments
 (0)