I am converting a .NET Core 3.1 web application from Couchbase 2.7.x client to 3.2.x and am having trouble with PersistentDictionary. I created a simple program to test inserting data, but cannot add more than1 item to the dictionary; throws “Couchbase.Core.Exceptions.KeyValue.PathExistsException”.
Any ideas?
Thanks,
T
try
{
using (var cluster = await Cluster.ConnectAsync("couchbase://server", "Administrator", "password"))
{
var bucket = await cluster.BucketAsync("test-data").ConfigureAwait(false);
var collection = await bucket.DefaultCollectionAsync().ConfigureAwait(false);
var dict = collection.Dictionary<Foo>("test-dictionary");
await dict.ClearAsync().ConfigureAwait(false);
await dict.AddAsync("Tom", new Foo { Name = "Tom", Age = 50 });
await dict.AddAsync("Dick", new Foo { Name = "Dick", Age = 30 });
}
}
catch(Exception ex)
{
Console.WriteLine($"Exception - {ex.GetType()}, {ex.Message}");
}
type or paste code here
Thanks, updated my test project this morning and can now add items to the PersistentDictionary.
I have run into a new problem when iterating over the dictionary.
Exception - Newtonsoft.Json.JsonSerializationException, Could not create an instance of type System.Collections.Generic.IEnumerator`1[System.Collections.Generic.KeyValuePair`2[System.String,TestCouchbaseCollection.Foo]]. Type is an interface or abstract class and cannot be instantiated. Path 'Fred', line 1, position 8.
Thanks,
T
using (var cluster = await Cluster.ConnectAsync("couchbase://dev-docker01", "Administrator", "password"))
{
var bucket = await cluster.BucketAsync("sms-data").ConfigureAwait(false);
var collection = await bucket.DefaultCollectionAsync().ConfigureAwait(false);
var dict = collection.Dictionary<Foo>("testdatadict");
await dict.ClearAsync().ConfigureAwait(false);
await dict.AddAsync("Fred", new Foo { Name = "Tom", Age = 50 });
await dict.AddAsync("Bill", new Foo { Name = "Dick", Age = 30 });
foreach (var item in dict)
{
Console.WriteLine($"{item.Key} - {item.Value.Name}, {item.Value.Age}");
}
}
}
During testing, I noticed that using the “Item” property to update existing data in the collection throws a “System.ArgumentException - An element with the same key already exists in the Dictionary”
using (var cluster = await Cluster.ConnectAsync("couchbase://server", "Administrator", "password"))
{
var bucket = await cluster.BucketAsync("test").ConfigureAwait(false);
var collection = await bucket.DefaultCollectionAsync().ConfigureAwait(false);
var dict = collection.Dictionary<Foo>("testdatadict");
await dict.ClearAsync().ConfigureAwait(false);
await dict.AddAsync("Tom", new Foo { Name = "Tom", Age = 50 }).ConfigureAwait(false);
await dict.AddAsync("Dick", new Foo { Name = "Dick", Age = 30 }).ConfigureAwait(false);
await dict.AddAsync("Harry", new Foo { Name = "Harry", Age = 40 }).ConfigureAwait(false);
// Get Tom from the Dictionary
var key = "Tom";
var item = dict[key];
// Update Age
item.Age = 55;
dict[key] = item;
await Task.Delay(1000).ConfigureAwait(false);
}
The team typically doesn’t release the Extensions packages every time they release the main SDK if there are no changes. CouchbaseNetClient 3.2.6 may be safely used with Couchbase.Extensions.DependencyInjection 3.2.5.