首页 > 解决方案 > 构建配置后如何更新 IOptions?

问题描述

上下文: 在集成测试中,我们需要使用不同的选项来测试不同的案例。我不想为每个测试创建一个不同的 TestServerFixture。

请求: 我正在寻找全局更新 IOptions 的可能性。

测试服务器夹具:

public Dictionary<string, string> Data {get; set;} = new Dictionary<string, string>
        {
            {"identity:claimsidentity:roleclaimtype", roleClaimType},
            {"identity:claimsidentity:usernameclaimtype", usernameClaimType},
            {"identity:claimsidentity:useridclaimtype", useridClaimType},
            {"identity:claimsidentity:securitystampclaimtype", securityStampClaimType},
            {"identity:user:requireUniqueEmail", "true"},
            {"identity:password:RequiredLength", "10"},
            {"identity:password:RequireNonAlphanumeric", "false"},
            {"identity:password:RequireUpperCase", "false"},
            {"identity:password:RequireDigit", "false"},
            {"identity:password:RequireLowerCase", "false"},
            {"identity:lockout:AllowedForNewUsers", "FALSe"},
            {"identity:lockout:MaxFailedAccessAttempts", "1000"}
        };

[..]

var hostBuilder = new HostBuilder()
    .ConfigureAppConfiguration((hostingContext, builder) =>
    {
        builder
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .AddInMemoryCollection(Data);
    })
    [..]

测试:

    [Fact]
    public async Task Should_Do_Something()
    {
        // When I do a call to the server, identity:user:requireUniqueEmail will be true


        TestServerFixture.Data = //New Data with identity:user:requireUniqueEmail  = false;

        // When I do a call to the server, identity:user:requireUniqueEmail will be false
    }

因此,每次我为属性 Data 设置一个新值时,它都会自动更新当前配置。

你知道怎么做吗?

标签: c#.net-core

解决方案


推荐阅读