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

Bugfix ms di no available scope #662

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Test for #646: resolve from thread pool causes null reference in scope
AGiorgetti authored and rvdginste committed Aug 6, 2023
commit 36db48029ee865e16ddde82bfd662b5181185990
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);
}
}
}