首页 > 解决方案 > 如何在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, ?? )); }

标签: asp.net-web-apiconfiguration

解决方案


嗨,您必须首先像这样注入其他作为构造函数的参数传递的服务

services.AddLogging(configure => configure.AddConsole())
  .AddTransient<iuserrepository>(sp => new UserRepository(connectionString, sp.GetService<ilogger<userrepository>>()))

在将这些服务注入到 PhysicalFileAttachmentService 之前,希望您从代码中得到想法


推荐阅读