srzsanti
srzsanti

Reputation: 300

How to initialize Singleton with its dependencies?

I am trying to Force the immediate instantiation or a Singleton in .Net 6. The reason I want to do this is because I want to load some data the moment that the application starts. I don't know if my approach is correct and if it is possible. Suggestions are more than welcome.

This is what I have tried so far.

My program.cs configuration

builder.Services.AddScoped<IIpLocationService, IpLocationService>();

var serviceProvider = builder.Services.BuildServiceProvider();
var memoryCacheProvider = new MemoryCacheProvider(
    serviceProvider.CreateScope().ServiceProvider
        .GetService<IIpLocationService>());
builder.Services.AddSingleton<IMemoryCacheProvider>(memoryCacheProvider)

But I get this error.

System.InvalidOperationException: 'Unable to resolve service for type 'Auth.In_MemoryDatabase.IMemoryCacheProvider' while attempting to activate 'Auth.Services.IpLocationService'.'

IIpLocationService has its own dependencies too. Namely a unitofwork in charge of retrieving the information from a database and the MemoryCacheProvider class to store the information in-memory.

This is my code of the MemoryCacheProvider

public interface IMemoryCacheProvider
{
    List<IpLocation> IpLocationList { get; set; }
}

public class MemoryCacheProvider : IMemoryCacheProvider
{
    private readonly IIpLocationService _ipLocationService;
    public List<IpLocation> IpLocationList { get; set; } = new List<IpLocation>();


    public MemoryCacheProvider(IIpLocationService ipLocationService)
    {
        _ipLocationService = ipLocationService;
        Init();
    }

    private void Init()
    {
        // do something with it
        _ipLocationService.Init();

    }
}

Is there a way I can make this work or something similar? I just need the data to start loading the moment the application starts because it's a lot of data. For this reason, I must be able to call the Init method of the IpLocationService.

Upvotes: 1

Views: 894

Answers (1)

Joost00719
Joost00719

Reputation: 754

I am not quite sure what you are trying to accomplish

Is there a way I can make this work or something similar? I just need the data to start loading the moment the application starts because it's a lot of data. For this reason, I must be able to call the Init method of the IpLocationService.

Maybe you can take a look in a Hosted service (IHostedService). This code will run before requests can be received. This way you can inject your dependancies in the hosted service, and fetch fetch your data. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio#startasync

Alternatively, you can run it in your Program.cs You can get your services after building the Host, but before you run it.

var host = CreateHostBuilder(args).Build();
host.Services.GetRequiredService<...>(...);
host.Run();

I hope this gives you some ideas on how to solve your issue.

Upvotes: 2

Related Questions