Blazor WASM Lasy loading DI

prof cegep 41 Reputation points
2023-05-29T04:01:25.1633333+00:00

Hello.

For DI with LAZY LOADING I need to call my service. BUT how to pass the expected ILogger ?

public class LazyLoadedServices
{
    private readonly IServiceProvider _services;


    public LazyLoadedServices(IServiceProvider services)
	{
        this._services = services;
   
    }

    public IFicheEspaceClosDataService Create()
    {
        HttpClient http = this._services.GetRequiredService<HttpClient>();
        return new FicheDataService(http, ?????????????);
    }
}

See the ??????????????

And my service class :

public class FicheDataService : IFicheDataService
{
    private readonly HttpClient _httpClient;
    private readonly ILogger<FicheService> _logger;
    public FicheDataService(HttpClient httpClient, ILogger<FicheDataService> logger)
    {
        _httpClient = httpClient;
        _logger = logger;
    }

Thanks a lot.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,495 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,091 Reputation points Microsoft Vendor
    2023-05-29T07:59:30.0533333+00:00

    Hi @prof cegep

    For DI with LAZY LOADING I need to call my service. BUT how to pass the expected ILogger ?

    You can modify your code as blow:

        public interface IFicheDataService
        {
            string GetData();
        }
    
        public class FicheDataService : IFicheDataService
        {
            private readonly HttpClient _httpClient;
            private readonly ILogger<FicheDataService> _logger;
            public FicheDataService(HttpClient httpClient, ILogger<FicheDataService> logger)
            {
                _httpClient = httpClient;
                _logger = logger;
            }
            public string GetData()
            {
                _logger.LogInformation("FicheDataService is called ");
                return "OK";
            }
        }
    
        public class LazyLoadedServices
        {
            private readonly IServiceProvider _services;
            private readonly ILogger<FicheDataService> _logger;
    
            public LazyLoadedServices(IServiceProvider services, ILogger<FicheDataService> logger)
            {
                this._services = services;
                _logger = logger;
            }
    
            public FicheDataService Create()
            {
                _logger.LogInformation("LazyLoadedServices is called ");
                HttpClient http = this._services.GetRequiredService<HttpClient>();
                return new FicheDataService(http, _logger);
            }
        }
    

    Then, in the Program.cs file, register the service like this:

    builder.Services.AddScoped<IFicheDataService, FicheDataService>();
    builder.Services.AddScoped<LazyLoadedServices>();
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments