Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How does the Func delegate work when it requires an interface as a parameter?

Specifically, I'm looking at this line of code from Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.cs:

public static IServiceCollection AddScoped<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory) where TService : class;

An example using this method would be:

services.AddScoped<ICustomService>(sp => new CustomService(
    sp.GetRequiredService<IAnotherCustomService>(), "Param1", "Param2"));

I understand how the Func delegate and lambda expressions work, but I'm not understanding how IServiceProvider is being initialized behind the scenes.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

IServiceProvider is not being initialized behind the scenes at this point in time. The framework is merely capturing the passed-in delegate and saving it for a later time, when it has an IServiceProvider instance and needs to generate an ICustomService.

There is nothing specific to interfaces happening here. The same principle would apply with any argument type for a delegate.

// This captures the delegate in a variable
Func<int, string> f = i => i.ToString();

// This invokes the delegate with an instance of an `int`
f(1);
f(2);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda