How to get list of registered services and run it manually?

winanjaya 146 Reputation points
2023-02-07T01:03:03.3733333+00:00

Hi,

How to get list of registered services and run it manually?

            var app = WebApplication.Create();

            using var scope = app.Services.CreateScope();
            var services = scope.ServiceProvider;
            var itest = services.GetRequiredService<ITest>();
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

4 answers

Sort by: Most helpful
  1. Adrian Gallo 80 Reputation points
    2023-02-07T05:12:53.6833333+00:00

    Hi @winanjaya could you try modifying your code to:

    var app = WebApplication.Create();
    
    using var scope = app.Services.CreateScope();
    var services = scope.ServiceProvider;
    
    var serviceList = services.GetServices<object>();
    
    foreach (var service in serviceList) 
    {
        Console.WriteLine(service.GetType().FullName);
    }
    
    var itest = services.GetRequiredService<ITest>();
    
    
    0 comments No comments

  2. Naimish Makwana 175 Reputation points
    2023-02-07T05:25:19.9533333+00:00

    Hello winanjaya,

    Please try below code.

    var app = WebApplication.Create();
    
    using var scope = app.Services.CreateScope();
    var services = scope.ServiceProvider;
    var serviceList = services.GetServices<ITest>();
    
    foreach (var service in serviceList)
    {
        service.Run();
    }
    

    Thanks

    Naimish Makwana

    0 comments No comments

  3. Naimish Makwana 175 Reputation points
    2023-02-07T05:26:54.42+00:00

    Hello winanjaya,

    Please try the code below.

    var app = WebApplication.Create();
    
    using var scope = app.Services.CreateScope();
    var services = scope.ServiceProvider;
    var serviceList = services.GetServices<ITest>();
    
    foreach (var service in serviceList)
    {
        service.Run();
    }
    

    Thanks

    Naimish Makwana

    0 comments No comments

  4. winanjaya 146 Reputation points
    2023-02-07T06:37:39.6966667+00:00

    I have tried it, it returns empty.

    below is one of my service that I put it in program.cs

    builder.Services.AddSingleton<ITest, Test>();
    builder.Services.AddHttpClient<ITest, Test>("test", c1 =>
    {
        c1.BaseAddress = new Uri("https://xxx.xxxxx.com/api");
        c1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        c1.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };
        c1.Timeout = TimeSpan.FromMinutes(3);
    })
        .SetHandlerLifetime(TimeSpan.FromMinutes(3))
        .ConfigureHttpClient((c2) =>
            new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain,
                sslPolicyErrors) =>
                {
                    return sslPolicyErrors == SslPolicyErrors.None;
                }
            }
        );
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.