首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在自己的ServiceCollection中获取ServiceCollection

在自己的ServiceCollection中获取ServiceCollection
EN

Stack Overflow用户
提问于 2019-07-19 15:40:35
回答 1查看 8.5K关注 0票数 6

我在做fixture到我的tests

在我的FixtureFactory我做我自己的ServiceCollection

代码语言:javascript
运行
复制
    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;
    }

然后,我从scope那里得到这个scope

代码语言:javascript
运行
复制
    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的如下所示:

代码语言:javascript
运行
复制
    private readonly IConfiguration configuration;

    private readonly UserManager<ApplicationUser> userManager;

    public TokenService(IConfiguration configuration, UserManager<ApplicationUser> userManager)
    {
        this.configuration = configuration;
        this.userManager = userManager;
    }

当我启动我的Core应用程序时,它工作得很好,但不会像我前面提到的那样在ServiceCollection上工作。

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

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

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

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

我使用Configurationsettings.json获取项目。

编辑:

我的fixture和示例测试类:

代码语言:javascript
运行
复制
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>
    {
    }
}

测试:

代码语言:javascript
运行
复制
[Collection("ServicesTestCollection")]
public class CreateProjectCommandTests
{
    private readonly TaskManagerDbContext context;

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

EDIT2

当我从集合中删除AddScoped<IConfiguration>();并运行测试时,会得到以下错误:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-19 16:15:25

您添加了没有实现的IConfiguration

在startup.cs中,框架会在幕后创建一个实现并添加它。您必须使用ConfigurationBuilder进行同样的操作。

代码语言:javascript
运行
复制
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);

//...
票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57115707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档