宇航员(Cosmos DB)-输入的有效Base-64字符串无效

时间:2019-10-27 15:10:42

标签: c# azure-cosmosdb azure-cosmosdb-sqlapi

我尝试编写通用的cosmos Db类,这在Cosmonaut CosmosDb包装器的帮助下有很多集合时会很有用。这是Factory类的示例

 public class CosmosDbClientFactory: ICosmosDbClientFactory
{
    private readonly string _databaseName;
    private readonly string _endpointUrl;
    private readonly string _masterKey;

    public CosmosDbClientFactory(string databaseName, string endpointUrl, string masterKey)
    {
        _databaseName = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
        _endpointUrl = endpointUrl ?? throw new ArgumentNullException(nameof(endpointUrl));
        _masterKey = masterKey ?? throw new ArgumentNullException(nameof(masterKey));
    }

    public CosmosStoreSettings GetCosmosStoreSettings()
    {
        var cosmosStoreSettings = new CosmosStoreSettings(
            databaseName: _databaseName,
            endpointUrl: _endpointUrl,
            authKey: _masterKey );
        return cosmosStoreSettings;
    } 

我的存储库类是

public abstract class CosmosDbRepository<T> where T: Entity
{
    private readonly ICosmosDbClientFactory _cosmosDbClientFactory;

    protected CosmosDbRepository(ICosmosDbClientFactory cosmosDbClientFactory)
    {
        _cosmosDbClientFactory = cosmosDbClientFactory;
    }

    public Task<string> AddAsync(T entity)
    {
        try
        {
            entity.Id = GenerateId(entity);
            ICosmosStore<T> cosmosStore = new CosmosStore<T>(_cosmosDbClientFactory.GetCosmosStoreSettings());
            var response = cosmosStore.AddAsync(entity);
            return Task.FromResult(entity.Id);
        }
        catch (Exception e)
        {
            Debug.WriteLine("Failed to save data into CosmosDb");
            throw;
        }
    } 
}

当我尝试使用该存储库时,出现以下错误

  

“输入内容不是有效的Base-64字符串,因为它包含非Base 64字符,   超过两个填充字符,或填充字符中有一个非法字符。”

从行

 ICosmosStore<T> cosmosStore = new CosmosStore<T>(_cosmosDbClientFactory.GetCosmosStoreSettings());

我想知道是否有人可以帮助我找出原因?

1 个答案:

答案 0 :(得分:1)

根据评论,问题的根源是所提供的_masterKey不是有效的Base 64字符串,也不是有效的Cosmos DB密钥。