forked from castleproject/Windsor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: castleproject#646 Castle.Windsor.Extension.DepencencyInjection …
…removed null check on scope cache (AsyncLocal can be null on Threads coming from Threadpool.UnsafeQueueUserWorkItem, having no null check was also the original behavior)
- Loading branch information
1 parent
77216d1
commit 08ad3dc
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/Castle.Windsor.Extensions.DependencyInjection.Tests/ResolveFromThreadpoolUnsafe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Castle.MicroKernel.Registration; | ||
using Castle.Windsor.Extensions.DependencyInjection.Tests.Components; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Castle.Windsor.Extensions.DependencyInjection.Tests { | ||
public class ResolveFromThreadpoolUnsafe { | ||
[Fact] | ||
public async Task Can_Resolve_From_ServiceProvider() { | ||
|
||
var serviceProvider = new ServiceCollection(); | ||
var container = new WindsorContainer(); | ||
var f = new WindsorServiceProviderFactory(container); | ||
f.CreateBuilder(serviceProvider); | ||
|
||
container.Register( | ||
Component.For<IUserService>().ImplementedBy<UserService>() | ||
); | ||
|
||
IServiceProvider sp = f.CreateServiceProvider(container); | ||
|
||
var actualUserService = sp.GetService<IUserService>(); | ||
Assert.NotNull(actualUserService); | ||
|
||
/* | ||
ThreadPool.UnsafeQueueUserWorkItem(state => { | ||
// resolving using castle (without scopes) works | ||
var actualUserService = container.Resolve<IUserService>(nameof(UserService)); | ||
Assert.NotNull(actualUserService); | ||
}, null); | ||
*/ | ||
|
||
TaskCompletionSource<IUserService> tcs = new TaskCompletionSource<IUserService>(); | ||
|
||
ThreadPool.UnsafeQueueUserWorkItem(state => { | ||
try { | ||
var actualUserService = sp.GetService<IUserService>(); | ||
Assert.NotNull(actualUserService); | ||
} | ||
catch (Exception ex) { | ||
tcs.SetException(ex); | ||
return; | ||
} | ||
tcs.SetResult(actualUserService); | ||
}, null); | ||
|
||
// Wait for the work item to complete. | ||
var task = tcs.Task; | ||
IUserService result = await task; | ||
Assert.NotNull(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters