I can't figure out how to share my service registrations between my test code that uses Orleans and the test code that is outside of Orleans. I've spent days trying to figure this out.
My unit tests have their own ServiceCollection where I've registered the services that my application requires. For my experiment, I'm registering one singleton (Something
).
I'm now adding Orleans' TestClusterBuilder to my unit test. According to the docs, I need to AddSiloBuilderConfigurator...
.
How do I avoid registering Something
twice? This feels like a chicken-and-egg situation. Worse still, the Something
that gets injected in my grains is a different singleton than the Something
that gets registered in app
.
This only seems to be a problem in my unit tests. In the main application code, there is no TestClusterBuilder
and the DI works as expected.
My code looks something like:
[SetUp]
public void Setup()
{
var webApplicationBuilder = WebApplication.CreateBuilder();
webApplicationBuilder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(c =>
{
c.RegisterType<Something>().AsSelf().SingleInstance();
});
var app = webApplicationBuilder.Build();
var testClusterBuilder = new TestClusterBuilder(1);
testClusterBuilder.AddSiloBuilderConfigurator<TestSiloConfigurations>();
TestCluster = testClusterBuilder.Build();
TestCluster.Deploy();
}
public class TestSiloConfigurations : ISiloConfigurator
{
public void Configure(ISiloBuilder siloBuilder)
{
siloBuilder.ConfigureServices(services =>
{
services.AddSingleton<Something>();
});
}
}
For the record, we're using Autofac, but I don't think that matters here.