// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IMyService, MyService>();
}// MyController.cs
public class MyController : Controller
{
private readonly IMyService _myService;
public MyController(IMyService myService)
{
_myService = myService;
} // ...
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IMyService, MyService>();
}
// MyController.cs
public class MyController : Controller
{
private readonly IMyService _myService;
public MyController(IMyService myService)
{
_myService = myService;
}
// ...
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IMyService, MyService>();
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IMySingletonService, MySingletonService>();
services.AddTransient<IMyTransientService, MyTransientService>();
services.AddScoped<IMyScopedService, MyScopedService>();
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IMyService, MyService>();
}
// MyController.cs
public class MyController : Controller
{
private readonly IMyService _myService;
public MyController(IMyService myService)
{
_myService = myService;
}
// ...
}
// MyController.cs
public class MyController : Controller
{
public IActionResult MyAction([FromServices] IMyService myService)
{
// ...
}
}
// MyService.cs
public class MyService : IMyService
{
[FromServices]
public ILogger<MyService> Logger { get; set; }
// ...
}