Call a service and return a value within that method that subsequent services depend on without using .BuildServiceProvider?

Viewed 70

Context:

  • Using .NET dependency injection (.NET Core 3.1)
  • WebHostBuilder, using Startup.cs to do configuration
  • Using ConfigureServices(IServiceCollection services) to configure the main services

Within this method, I need to call the method of a singleton service like so...

services.AddSingleton((sp) =>
{
    var svc = sp.GetRequiredService<Dependency_A>();
    var setConfig = new ConfigurationDataClass(svc);
    setConfig = setConfig.CallMethod().Result;
    return setConfig;
});

//-- Need to get value stored in setConfig here somehow without using 
//-- services.BuildServiceProvider()
//-- Because it creates a whole other container which I want to avoid doing

//-- After getting setConfig.OtherData
//-- I'll use to pass other data to subsequent services that need it

Unfortunately, due to the constraints of the existing design structure there is no way for me to change the way the latter services receive the data from the configuration data service, I’m limited to finding a blocking call that allows me to avoid creating a duplicate container. Any advice is appreciated, as I’m wanting to exhaust all possibilities.

1 Answers

Answering my own question: The solution was simply to reorganize the surrounding dependent services so that, I’m happy to say, I successfully avoided calling .BuildServiceProvider(). Yay.

Related