C# IOptionsMonitor<;T>;缺少appsetting值时,OptionAccessor null

C# IOptionsMonitor<;T>;缺少appsetting值时,OptionAccessor null,c#,.net-core,asp.net-core-3.1,ioptionsmonitor,C#,.net Core,Asp.net Core 3.1,Ioptionsmonitor,我对配置设置有一个小问题,当缺少\空值时,“IOptionsMonitor”CurrentValue为null。 启动代码是 var keySection = Configuration.GetSection("BlobStorageAccountKeys"); services.Configure<AzureBlobKeyConfiguration>(keySection); 这映射到 public class AzureBlobKeyConfiguratio

我对配置设置有一个小问题,当缺少\空值时,“IOptionsMonitor”CurrentValue为null。 启动代码是

var keySection = Configuration.GetSection("BlobStorageAccountKeys");
services.Configure<AzureBlobKeyConfiguration>(keySection);
这映射到

public class AzureBlobKeyConfiguration
{
    public AccountNameWithKey[] Tenants { get; set; }
}

public class AccountNameWithKey
{
    public string AccountName { get; set; }

    public string AccountKey { get; set; }

    public Guid DocumentTenantId { get; set; }
}
运行此方法时,我得到一个空异常错误

public class AzureBlobAsyncFileStore : IAsyncFileStore
{
    private readonly IAzureBlobServiceClientFactory _azureBlobServiceClientFactory;
    private readonly IOptionsMonitor<AzureBlobKeyConfiguration> _optionsAccessor;

    public AzureBlobAsyncFileStore(IAzureBlobServiceClientFactory azureBlobServiceClientFactory, IOptionsMonitor<AzureBlobKeyConfiguration> optionsAccessor)
    {
        _azureBlobServiceClientFactory = azureBlobServiceClientFactory ?? throw new ArgumentNullException(nameof(azureBlobServiceClientFactory));
        _optionsAccessor = optionsAccessor;
    }

    public async Task<string> StoreFileAsync(UploadToStorageRequest request, CancellationToken cancellationToken)
    {
        var accountDetails = _optionsAccessor.CurrentValue.Tenants; 
        var account = accountDetails.ToList().FirstOrDefault(account => account.DocumentTenantId == request.DocumentStoreTenantId); // If a value is missing in any apart of the config it has a null object and then throws a null excpetion reference 
        /// Code ommited
    }
}

您是说当配置中的
DocumentTenantId
为空字符串时,将填充
AzureBlobKeyConfiguration.Tenants
数组,但是该数组中的相关租户是
null
?@IanKemp我已经编辑了这个问题,以显示我在验证器中看到的与显示的配置相同的图像above@IanKemp编辑现在应该在那里,不知道为什么在你的屏幕截图显示出与问题中不同的代码之前不需要编辑?另外,您是否在其他地方配置了
AzureBlobKeyConfiguration
?@poke我已更新,因此请在同样存在问题的图像中引用代码,不,这是唯一配置此问题的位置
public class AzureBlobAsyncFileStore : IAsyncFileStore
{
    private readonly IAzureBlobServiceClientFactory _azureBlobServiceClientFactory;
    private readonly IOptionsMonitor<AzureBlobKeyConfiguration> _optionsAccessor;

    public AzureBlobAsyncFileStore(IAzureBlobServiceClientFactory azureBlobServiceClientFactory, IOptionsMonitor<AzureBlobKeyConfiguration> optionsAccessor)
    {
        _azureBlobServiceClientFactory = azureBlobServiceClientFactory ?? throw new ArgumentNullException(nameof(azureBlobServiceClientFactory));
        _optionsAccessor = optionsAccessor;
    }

    public async Task<string> StoreFileAsync(UploadToStorageRequest request, CancellationToken cancellationToken)
    {
        var accountDetails = _optionsAccessor.CurrentValue.Tenants; 
        var account = accountDetails.ToList().FirstOrDefault(account => account.DocumentTenantId == request.DocumentStoreTenantId); // If a value is missing in any apart of the config it has a null object and then throws a null excpetion reference 
        /// Code ommited
    }
}
public class UploadToStorageValidator : AbstractValidator<UploadToStorageRequest>
{
    private readonly IOptionsMonitor<AzureBlobKeyConfiguration> _optionsAccessor;

    public UploadToStorageValidator(IOptionsMonitor<AzureBlobKeyConfiguration> optionsAccessor)
    {
        _optionsAccessor = optionsAccessor;

        RuleFor(command => command.DocumentStoreTenantId).NotNull().NotEmpty().DependentRules(() =>
        {
            RuleFor(command => command.DocumentStoreTenantId).Custom(CheckTenantExists);
        });
    }

    private void CheckTenantExists(Guid tenantId, CustomContext customContext)
    {
        var tenants = _optionsAccessor.CurrentValue.Tenants;
        if (!tenants.Any(tenant => tenant.DocumentTenantId == tenantId))
        {
            customContext.AddFailure($"No tenant exists with Id : {tenantId}");
        }
    }
}