Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i reference a singleton instance i'm adding in the HostBuilder, as a part of a Scoped instance i'm defining?

I may be barking up the wrong tree here, but, i have the following:

var host = new HostBuilder() 
    .ConfigureServices(services => 
    {
        services.AddSingleton<IHttpClientFactory>();
        services.AddScoped<IPaintMapper, PaintMapper(XXXXX, config)>();
    };

I want to pass the IHttpClientFactory into my Scoped "PaintMapper", which i will provide also a config for.

How would i do this? As the IHttpClientFactory and the "config" are both required to setup the scoped instance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve been at this refactor for a while and think my brain is not handling it particularly well, so my apologies if i’m missing something normal – but i don’t see this done elsewhere, so i’m probably missing something.

>Solution :

You can do like this

services.AddScoped<IPaintMapper, PaintMapper>(provider => {
    var config = provider.GetRequiredService<Config>();
    var httpFactory = provider.GetRequiredService<IHttpClientFactory>();
    // Do stuff with mapper
    return mapper;   
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading