【发布时间】:2016-04-27 16:14:44
【问题描述】:
有没有办法实现下一个行为?
public static void Configure(IServiceCollection services) {
services.AddScoped(typeof(Func<IPrincipal>), ???);
services.AddInstance(typeof(Func<IPrincipal>), ???);
}
1.不起作用:
Func<IServiceProvider, IPrincipal> getPrincipal =
(sp) => sp.GetService<IHttpContextAccessor>().HttpContext.User;
services.AddScoped(
typeof(Func<IPrincipal>),
getPrincipal);
2.不起作用:
var builder = services.BuildServiceProvider();
services.AddInstance(
typeof(Func<IPrincipal>),
builder.GetService<IHttpContextAccessor>().HttpContext.User);
【问题讨论】:
-
你想达到什么目的?目前尚不清楚您需要什么最终结果。例如,您是否想要一个返回当前用户的服务?
标签: c# asp.net dependency-injection asp.net-core