asp.net-web-api - 如何在startup.cs的ConfigureServices方法中注入UserManager?
问题描述
如何将 UserManager 实例传递给调用的 PhysicalFileAttachmentService 服务?
这是代码
public class PhysicalFileAttachmentService : IFileAttachmentService {
private readonly ILogger<PhysicalFileAttachmentService> _logger;
private readonly string _contentRootPath;
private readonly UserManager<ApplicationUser> _userManager;
public PhysicalFileAttachmentService(ILogger<PhysicalFileAttachmentService> logger, string contentRootPath, UserManager<ApplicationUser> userManager)
{
_logger = logger;
_contentRootPath = contentRootPath;
_userManager = userManager;
} }
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<IFileAttachmentService>(s => new PhysicalFileAttachmentService(s.GetService<ILogger<PhysicalFileAttachmentService>>(), s.GetService<IHostingEnvironment>().ContentRootPath, ?? )); }
解决方案
嗨,您必须首先像这样注入其他作为构造函数的参数传递的服务
services.AddLogging(configure => configure.AddConsole())
.AddTransient<iuserrepository>(sp => new UserRepository(connectionString, sp.GetService<ilogger<userrepository>>()))
在将这些服务注入到 PhysicalFileAttachmentService 之前,希望您从代码中得到想法
推荐阅读
- ios - 如何从带有 JSON 数据的字符串中获取 UIImage
- javascript - 如何使用 jasmine 测试测试数据切换(引导程序)?
- python - python pandas中的反向布尔列
- javascript - How to handle props and states to avoid infinite loops?
- mysql - How to return a default column value for mass assignment of a model?
- angular - 没有jquery向下滚动时粘性菜单会改变颜色
- asp.net - 如何跳过路由的全局消息处理程序?
- python - 正则表达式模式仅用一个条目替换列值
- design-patterns - 使用 ABAC,如何表达允许主体 A 与主体 B 共享资源 X 的规则?
- javascript - Electron Webpack 中 Main 和 Renderer 文件夹的用途是什么