为什么IServiceCollection.AddScoped<TService, TImplementation>() 不能得到TImplementation?
Why IServiceCollection.AddScoped<TService, TImplementation>() cannot get TImplementation?
这是 .net core 3.1 上的演示 运行
当通过provider.GetRequiredService()方法构建对象时,它会调用HomeController'constructor。显然,HomeController'constructor必须在其中输入一个对象,该对象是“GreetingService”生成的实例。
在从容器创建对象的过程中,生成“GreetingService”的实例似乎是必须的,natural.But为什么我们不能调用方法provider.GetRequiredService() 并获取实例?
using System;
using Microsoft.Extensions.DependencyInjection;
namespace DIDemo
{
class Program
{
static void Main(string[] args)
{
ServiceCollection service = new ServiceCollection();
service.AddScoped<IGreetingService, GreetingService>();
service.AddScoped<HomeController>();
var provider = service.BuildServiceProvider();
//var result = provider.GetRequiredService<GreetingService>(); why wrong
var result = provider.GetRequiredService<HomeController>();
result.Hello("Bob");
}
}
public class HomeController
{
private readonly IGreetingService _greetingService;
public HomeController(IGreetingService greetingService)
{
_greetingService = greetingService;
}
public string Hello(string name) => _greetingService.Greet(name);
}
public interface IGreetingService
{
string Greet(string name);
}
public class GreetingService : IGreetingService
{
public string Greet(string name)
{
Console.WriteLine($"Hello,{name}");
return "Done";
}
}
}
这是因为您已经注册了接口 - IGreetingService
,而不是 class GreetingService
。接下来将工作:
var result = provider.GetRequiredService<IGreetingService>();
如果您查看具有 2 个通用参数的 Add..
方法签名,您将看到 next:
public static IServiceCollection AddScoped<TService,TImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TService : class where TImplementation : class, TService;
所以在你的 GreetingService
案例中 TService
是 IGreetingService
而 TImplementation
是 GreetingService
,如文档中所述:
Adds a scoped service of the type specified in TService
with an implementation type specified in TImplementation
to the specified IServiceCollection
.
对于单个通用参数版本:
public static IServiceCollection AddScoped<TService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TService : class;
Adds a scoped service of the type specified in TService
to the specified IServiceCollection
.
这是 .net core 3.1 上的演示 运行 当通过provider.GetRequiredService()方法构建对象时,它会调用HomeController'constructor。显然,HomeController'constructor必须在其中输入一个对象,该对象是“GreetingService”生成的实例。
在从容器创建对象的过程中,生成“GreetingService”的实例似乎是必须的,natural.But为什么我们不能调用方法provider.GetRequiredService() 并获取实例?
using System;
using Microsoft.Extensions.DependencyInjection;
namespace DIDemo
{
class Program
{
static void Main(string[] args)
{
ServiceCollection service = new ServiceCollection();
service.AddScoped<IGreetingService, GreetingService>();
service.AddScoped<HomeController>();
var provider = service.BuildServiceProvider();
//var result = provider.GetRequiredService<GreetingService>(); why wrong
var result = provider.GetRequiredService<HomeController>();
result.Hello("Bob");
}
}
public class HomeController
{
private readonly IGreetingService _greetingService;
public HomeController(IGreetingService greetingService)
{
_greetingService = greetingService;
}
public string Hello(string name) => _greetingService.Greet(name);
}
public interface IGreetingService
{
string Greet(string name);
}
public class GreetingService : IGreetingService
{
public string Greet(string name)
{
Console.WriteLine($"Hello,{name}");
return "Done";
}
}
}
这是因为您已经注册了接口 - IGreetingService
,而不是 class GreetingService
。接下来将工作:
var result = provider.GetRequiredService<IGreetingService>();
如果您查看具有 2 个通用参数的 Add..
方法签名,您将看到 next:
public static IServiceCollection AddScoped<TService,TImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TService : class where TImplementation : class, TService;
所以在你的 GreetingService
案例中 TService
是 IGreetingService
而 TImplementation
是 GreetingService
,如文档中所述:
Adds a scoped service of the type specified in
TService
with an implementation type specified inTImplementation
to the specifiedIServiceCollection
.
对于单个通用参数版本:
public static IServiceCollection AddScoped<TService> (this Microsoft.Extensions.DependencyInjection.IServiceCollection services) where TService : class;
Adds a scoped service of the type specified in
TService
to the specifiedIServiceCollection
.