我想创建存储桶并将couchbase SDK与Testcontainers一起使用。在编写用于设置容器和创建存储桶的基本代码时,我得到的异常不是很详细。
Couchbase.Core.Exceptions.KeyValue.SocketNotAvailableException:ChannelConnectionPool
at Couchbase.Core.ClusterContext.GetOrCreateBucketLockedAsync(String name)
at Program.<Main>$(String[] args) in \ConsoleApp4\ConsoleApp4\Program.cs:line 24
重现代码: Csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CouchbaseNetClient" Version="3.4.12" />
<PackageReference Include="Testcontainers" Version="3.5.0" />
<PackageReference Include="Testcontainers.Couchbase" Version="3.5.0" />
</ItemGroup>
</Project>
程序.cs
using Couchbase;
using Testcontainers.Couchbase;
const string TestBucketName = "Test";
CouchbaseContainer CouchbaseContainer
= new CouchbaseBuilder()
.Build();
try
{
await CouchbaseContainer.StartAsync();
var options = new ClusterOptions
{
UserName = CouchbaseBuilder.DefaultUsername,
Password = CouchbaseBuilder.DefaultPassword,
};
var connectionString = CouchbaseContainer.GetConnectionString();
var cluster = await Cluster.ConnectAsync(connectionString, options);
var bucket = await cluster.BucketAsync(TestBucketName);
var collection = await bucket.DefaultCollectionAsync();
}
catch (Exception e)
{
await CouchbaseContainer.DisposeAsync();
Console.WriteLine(e);
}
我尝试从 UI 创建存储桶,它成功了。
编辑:经过一番挖掘,我发现造成这种情况的主要原因是
ChannelConnectionPool
在我想要创建存储桶之前被处置。这是该方法的调用堆栈:
at Couchbase.Core.IO.Connections.Channels.ChannelConnectionPool.Dispose()
at Couchbase.Core.ClusterNode.Dispose()
at Couchbase.Core.ClusterContext.BootstrapGlobalAsync()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.ExecutionContextCallback(Objects)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.<>c.<OutputWaitEtwEvents>b__12_0(Action innerContinuation, Task innerTask)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining)
at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)
at System.Threading.Tasks.Task.FinishContinuations()
at System.Threading.Tasks.Task1.TrySetResult(TResult result)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.SetExistingTaskResult(Task1task, TResult result)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.SetResult(TResult result)
at Couchbase.Core.ClusterNode.GetClusterMap(Nullable1configVersion)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.ExecutionContextCallback(Objects)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.<>c.<OutputWaitEtwEvents>b__12_0(Action innerContinuation, Task innerTask)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(Action action, Boolean allowInlining)
at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)
at System.Threading.Tasks.Task.FinishContinuations()
at System.Threading.Tasks.Task1.TrySetResult(TResult result)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.SetExistingTaskResult(Task1 task, TResult result)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.SetResult(TResult result)
at Couchbase.Core.ClusterNode.ExecuteOp(Func4 sender, IOperation op, Object state, CancellationTokenPair tokenPair)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread)
at System.Threading.ThreadPool.<>c.<.cctor>b__87_0(Object state)
at Couchbase.Core.IO.Operations.OperationBase.HandleOperationCompleted(SlicedMemoryOwner1& data)
at Couchbase.Core.IO.Operations.OperationBase1.Couchbase.Core.IO.Operations.IOperation.HandleOperationCompleted(SlicedMemoryOwner1& data)
at Couchbase.Core.IO.AsyncStateBase.SendResponseInternal(Object response)
at System.Threading.QueueUserWorkItemCallbackDefaultContext1.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
at System.Threading.Thread.StartCallback()
编辑2: 经过更多挖掘后,我发现 BootstrapGlobalAsync 方法中 ClusterContext 类中的这段代码:
if (GlobalConfig.UseAlternateAddresses)
{
node.Dispose();
}
它正在配置节点。将其硬编码为 false 后,它开始工作。
这似乎是
CouchbaseNetClient
版本3.4.12
中的一个错误。尽管我花了相当长的时间试图帮助@couchbaseuser但没有成功,但我再次尝试并检查了客户端版本。我遇到同样的错误。将客户端降级至 3.4.3
即可解决该问题。