建立配置后如何更新IOptions?

时间:2019-12-19 23:06:07

标签: c# .net-core

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

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

TestServerFixture:

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设置一个新值时,它都会自动更新当前配置。

您是否知道该怎么做?

0 个答案:

没有答案