visit
What's the result of the following code?
public interface IServiceA { }
class ServiceA : IServiceA
{
ServiceA()
{
Console.WriteLine("New SA");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IServiceA, ServiceA>();
...
}
}
System.AggregateException: 'Some services are not able to be constructed'
A suitable constructor for type 'AspNetCore.Services.ServiceA' could not be located.
Ensure the type is concrete and services are registered for all parameters of a public constructor.
Why not public class? Because the DI framework already gets the class by method call. If we want to access a class by using namespace, public is needed.
public interface IServiceA { }
public class ServiceA : IServiceA
{
public ServiceA()
{
Console.WriteLine("New SA");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServiceA, ServiceA>();
...
}
}
public class HelloController : ControllerBase
{
public WeatherForecastController(IServiceA sa)
{
Console.WriteLine($"Test Controller: {sa.GetHashCode()}");
}
}
The ServiceA won't be initialized. The DI framework,m will check the constructor (I guess), but won't initialize the instance. It is only when the singleton service is used that it will be initialized.
In this case, the service is depended by the controller, and the controller will be initialized for every request. So, as a singleton service, the ServiceA will be initialized when the first request comes. When other requests come after the first request, the singleton service won't be initialized any more:// 1st request
New SA
Test Controller: 83452835
// 2nd request
Test Controller: 83452835
// 3rd request
Test Controller: 83452835
// 1st request
New SA
Test Controller: 72334852
// 2nd request
New SA
Test Controller: 83729442
// 3rd request
New SA
Test Controller: 19231424
public interface IServiceA { }
public class ServiceA : IServiceA
{
public ServiceA()
{
Console.WriteLine("New SA");
}
}
public interface IServiceB { }
public class ServiceB : IServiceB
{
public ServiceB()
{
Console.WriteLine("New SB");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServiceA, ServiceA>();
services.AddSingleton<IServiceB, ServiceB>();
...
}
}
public class HelloController : ControllerBase
{
public WeatherForecastController(IServiceB sb, IServiceA sa)
{
Console.WriteLine($"Test Controller: {sa.GetHashCode()}");
}
}
New SB
New SA
Test Controller: 83427434 22834295
public class ServiceB : IServiceB
{
public ServiceB(IServiceA sa)
{
Console.WriteLine($"New SB with sa: {sa.GetHashCode()}");
}
}
New SA
New SB with sa: 46284926
Test Controller: 46284926 64753745
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IServiceB, ServiceB>();
services.AddScoped<IServiceA, ServiceA>();
}
New SA
New SB with sa: 72362183
Test Controller: 72362183 91218567
public interface IServiceA { }
public class ServiceA : IServiceA
{
public ServiceA()
{
Console.WriteLine("New SA");
}
}
public class ServiceB : IServiceA
{
public ServiceB()
{
Console.WriteLine("New SB");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServiceA, ServiceA>();
services.AddSingleton<IServiceA, ServiceB>();
...
}
}
public class HelloController : ControllerBase
{
public WeatherForecastController(IServiceA sa)
{
Console.WriteLine($"Test Controller: {sa.GetHashCode()}");
}
}
New SB
Test Controller: 46399782
public interface IServiceA { }
public interface IServiceB { }
public class ServiceB : IServiceA, IServiceB
{
public ServiceB()
{
Console.WriteLine("New SB");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServiceA, ServiceB>();
services.AddSingleton<IServiceB, ServiceB>();
...
}
}
public class HelloController : ControllerBase
{
public WeatherForecastController(IServiceA sa, IServiceB sb)
{
Console.WriteLine($"Test Controller: {sa.GetHashCode()} {sb.GetHashCode()}");
}
}
New SB
New SB
Test Controller: 78346274