gpt4 book ai didi

c# - 在自己的 ServiceCollection 中获取 IConfiguration

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:19 25 4
gpt4 key购买 nike

我正在制作 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;
}

然后,我得到这个 services来自 scope :

    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>();

当我添加 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;
}

当我启动我的 Core 时效果很好应用程序,但无法从 ServiceCollection 运行我在上面提到过。

在测试中,我开始遇到这个错误:

Message: System.AggregateException : One or more errors occurred. (Cannot instantiate implementation type 'Microsoft.Extensions.Configuration.IConfiguration' for service type 'Microsoft.Extensions.Configuration.IConfiguration'.) (The following constructor parameters did not have matching fixture data: ServicesFixture fixture) ---- System.ArgumentException : Cannot instantiate implementation type 'Microsoft.Extensions.Configuration.IConfiguration' for service type 'Microsoft.Extensions.Configuration.IConfiguration'. ---- The following constructor parameters did not have matching fixture data: ServicesFixture fixture

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

我的问题是,如何正确注册Configuration我在 Service 中使用?

我正在使用 Configurationsettings.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>
{
}
}

测试:

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

public CreateProjectCommandTests(ServicesFixture fixture)
{
this.context = fixture.Context;
}
}

EDIT2

当我删除 AddScoped<IConfiguration>(); 时从我的收集和运行测试中,我得到这个错误:

Message: System.AggregateException : One or more errors occurred. (No service for type 'TaskManager.Infrastructure.Implementations.TokenService' has been registered.) (The following constructor parameters did not have matching fixture data: ServicesFixture fixture) ---- System.InvalidOperationException : No service for type 'TaskManager.Infrastructure.Implementations.TokenService' has been registered. ---- The following constructor parameters did not have matching fixture data: ServicesFixture fixture

最佳答案

您添加了 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);

//...

关于c# - 在自己的 ServiceCollection 中获取 IConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57115707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com