如何在startup.cs的ConfigureServices方法中注入UserManager?

时间:2019-02-20 12:04:14

标签: asp.net-web-api configuration

如何将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, ?? )); }

1 个答案:

答案 0 :(得分:0)

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

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

在将这些服务注入PhysicalFileAttachmentService之前,希望您能从代码中获得灵感