Can the TestCluster be used to obtain items from the service collection?

85 Views Asked by At

I've been successfully using the TestCluster to get grain instances for testing. The cluster was configured with several services in its service collection. The service interfaces are injected into the grain classes. There is a ServiceProvider property on the test cluster instance and it has a GetService method so I thought that I could obtain these services this way from test case code. When I try, I get null for whatever service I ask for. No problem getting grain instances so I know at least that part of it working. I'm sure the services are present because the related tests that rely on the services are passing.

Is it even possible to retrieve the services this way? It doesn't seem like the ServiceProvider would even be there if there wasn't some intention that it can be used this way.

See also
Repost of question on Orleans repo Q&A
Can the TestCluster be used to obtain items from the service collection?

1

There are 1 best solutions below

2
On

If you're setting up the ClusterFixture on your own, you can make your your services easily available by implementing the ISiloConfigurator interface. From the official docs here: https://learn.microsoft.com/en-us/dotnet/orleans/implementation/testing

public class ClusterFixture : IDisposable
{
    public ClusterFixture()
    {
        var builder = new TestClusterBuilder();
        builder.AddSiloBuilderConfigurator<TestSiloConfigurations>();
        Cluster = builder.Build();
        Cluster.Deploy();
    }

    public void Dispose()
    {
        Cluster.StopAllSilos();
    }

    public TestCluster Cluster { get; }
}

public class TestSiloConfigurations : ISiloConfigurator
{
    public void Configure(ISiloBuilder siloBuilder)
    {
        siloBuilder.ConfigureServices(services =>
        {
            services.AddSingleton<T, Impl>(/* ... */);
        });
    }
}