.net core DI 注册 Lazy<> 类型
当我们在 .net core (2.1) 中运行如下代码注入 Lazy
public AccountService(LazyhttpContextAccessor) { }
可能会遇到这样的错误提示:
InvalidOperationException: Unable to resolve service for type 'System.Lazy`1[Microsoft.AspNetCore.Http.IHttpContextAccessor]' while attempting to activate 'Senparc.Service.AccountService'.
原因在于系统没有办法识别 Lazy
public void ConfigureServices(IServiceCollection services) { services.AddMvc();//默认会有 services.AddTransient(typeof(Lazy<>));//注册Lazy }
Lazy