private async Task ExecuteAsync(Func <DbCollection, Task> cb, CancellationToken cancellationToken = default ) { var dbCol = DbCollectionFactory.GetDatabaseAndCollection <TModel>(_options); while (true) { try { await cb(dbCol); break; } catch (DocumentClientException dce) { if (dce.StatusCode == HttpStatusCode.Conflict) { throw new EventStoreConcurrencyException($"A duplicated constraint (Unique Key) violation for {typeof(TModel).Name}."); } if (dce.StatusCode == HttpStatusCode.NotFound || dce.StatusCode == HttpStatusCode.Gone) { var createdDb = await _client.CreateDbColDatabaseAsync(_logger, dbCol, cancellationToken); var createdCol = await _client.CreateDbColCollectionAsync(_logger, dbCol, cancellationToken); if (createdDb || createdCol) { continue; } throw new EventStoreNotFoundException("Execution failure", dce); } if (dce.StatusCode == HttpStatusCode.ServiceUnavailable) { // Hmmm? this happens quite a bit locally. throw new EventStoreAccessException( "Cosmos is DOWN. Execution failure", dce ); } throw; } } }