在自己的 ServiceCollection 中获取 IConfiguration

mic*_*cer 4 c# dependency-injection .net-core asp.net-core asp.net-core-2.2

fixture对我的tests.

FixtureFactory我自己制作的即时消息中ServiceCollection

    private static ServiceCollection CreateServiceCollection()
    {
        var services = new ServiceCollection();

        services.AddScoped<IConfiguration>();
        services.AddScoped<ITokenService, TokenService>();
        services.AddDbContext<TaskManagerDbContext>(o => o.UseInMemoryDatabase(Guid.NewGuid().ToString()));
        services.AddIdentity<ApplicationUser, IdentityRole>(options =>
        {
            options.Password.RequiredLength = 6;
            options.Password.RequireLowercase = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireDigit = false;
            options.User.RequireUniqueEmail = true;
        }).AddEntityFrameworkStores<TaskManagerDbContext>();

        return services;
    }
Run Code Online (Sandbox Code Playgroud)

然后,我servicesscope

    var services = CreateServiceCollection().BuildServiceProvider().CreateScope();

    var context = services.ServiceProvider.GetRequiredService<TaskManagerDbContext>();
    var userManager = services.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
    var roleManager = services.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
    var signInManager = services.ServiceProvider.GetRequiredService<SignInManager<ApplicationUser>>();
    var tokenService = services.ServiceProvider.GetRequiredService<TokenService>();
Run Code Online (Sandbox Code Playgroud)

当我添加TokenService到我的ServiceCollection.

构造函数,TokenService看起来像这样:

    private readonly IConfiguration configuration;

    private readonly UserManager<ApplicationUser> userManager;

    public TokenService(IConfiguration configuration, UserManager<ApplicationUser> userManager)
    {
        this.configuration = configuration;
        this.userManager = userManager;
    }
Run Code Online (Sandbox Code Playgroud)

当我启动我的Core应用程序时它工作得很好,但不能从ServiceCollection我上面提到的那个工作。

在测试中,我开始收到此错误:

消息:System.AggregateException:发生一个或多个错误。(无法为服务类型“Microsoft.Extensions.Configuration.IConfiguration”实例化实现类型“Microsoft.Extensions.Configuration.IConfiguration”。)(以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具)---- System.ArgumentException : 无法为服务类型“Microsoft.Extensions.Configuration.IConfiguration”实例化实现类型“Microsoft.Extensions.Configuration.IConfiguration”。---- 以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具

当我添加services.AddScoped<IConfiguration>();到我的ServiceCollection以及开始获得所需的服务时,此错误开始显示TokenService

我的问题是,如何正确注册Configurationim 使用 in Service

Configuration用来从settings.json.

编辑:

我的fixture和示例测试类:

public class ServicesFixture
{
    public TaskManagerDbContext Context { get; private set; }

    public UserManager<ApplicationUser> UserManager { get; private set; }

    public RoleManager<IdentityRole> RoleManager { get; private set; }

    public SignInManager<ApplicationUser> SignInManager { get; private set; }

    public TokenService TokenService { get; private set; }

    public ServicesFixture()
    {
        var servicesModel = ServicesFactory.CreateProperServices();

        this.Context = servicesModel.Context;
        this.UserManager = servicesModel.UserManager;
        this.RoleManager = servicesModel.RoleManager;
        this.SignInManager = servicesModel.SignInManager;
        this.TokenService = servicesModel.TokenService;
    }

    [CollectionDefinition("ServicesTestCollection")]
    public class QueryCollection : ICollectionFixture<ServicesFixture>
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

测试:

[Collection("ServicesTestCollection")]
public class CreateProjectCommandTests
{
    private readonly TaskManagerDbContext context;

    public CreateProjectCommandTests(ServicesFixture fixture)
    {
        this.context = fixture.Context;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑2

当我AddScoped<IConfiguration>();从我的集合中删除并运行测试时,我收到此错误:

消息:System.AggregateException:发生一个或多个错误。(没有注册“TaskManager.Infrastructure.Implementations.TokenService”类型的服务。)(以下构造函数参数没有匹配的夹具数据:ServicesFixture fixture)----System.InvalidOperationException:没有类型“TaskManager.Infrastructure”的服务.Implementations.TokenService' 已注册。---- 以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具

Nko*_*osi 19

IConfiguration在没有实现的情况下添加了。

startup.cs 中,框架会在幕后创建一个实现并添加它。您必须使用相同的方法ConfigurationBuilder

var builder = new ConfigurationBuilder()
    //.SetBasePath("path here") //<--You would need to set the path
    .AddJsonFile("appsettings.json"); //or what ever file you have the settings

IConfiguration configuration = builder.Build();

services.AddScoped<IConfiguration>(_ => configuration);

//...
Run Code Online (Sandbox Code Playgroud)