DotNet SDK 3.0.7
I am trying some code samples right from SDK 3.0 documentation: https://docs.couchbase.com/dotnet-sdk/current/howtos/transcoders-nonjson.html#rawbinarytranscoder and getting InvalidOperationException:The RawBinaryTranscoder can only decode byte arrays on ContentAs. What am I missing here?
My complete code:
static async Task Main(string
ICluster _cluster = await Cluster.ConnectAsync(“couchbase://192.168.0.6”, “test”, “test”);
var docId = “doc”;
var bytes = System.Text.Encoding.UTF8.GetBytes(“hello world”);
var bucket = await _cluster.BucketAsync(“TEST”);
var _collection = bucket.DefaultCollection();
await _collection.UpsertAsync(docId, bytes, options => options.Transcoder(new RawBinaryTranscoder()));
var result = await _collection.GetAsync(docId, options => options.Transcoder(new RawBinaryTranscoder()));
var returned = result.ContentAs<byte >();
string s = System.Text.Encoding.UTF8.GetString(returned);
Looking at the SDK code, it appears that for the RawBinaryTranscoder to work the data must also be flagged as binary on the server. You might try using the RawStringTranscoder, since you’re decoding the byte array to a string anyway.
stream.Write(bytes, 0, bytes.Length);
throw new InvalidOperationException("The RawBinaryTranscoder can only encode byte arrays.");
public override T Decode<T>(ReadOnlyMemory<byte> buffer, Flags flags, OpCode opcode)
if (flags.DataFormat == DataFormat.Binary)
object value = DecodeBinary(buffer.Span);
return (T) value;
throw new InvalidOperationException("The RawBinaryTranscoder can only decode byte arrays.");
Looks like the server knows the data is binary. Also I need it for binary data. The string here is coming from SDK doc sample.
I believe that document is actually encoded as a Private data format (lower bits of flags = 1, not 3). However, regardless, it
It appears that it’s a known bug which has been fixed for the next release:
Until then, you might try the LegacyTranscoder. Looking at the logic I think it will work for you.
Off the topic but .RawStringTranscoder example from the same document. does not work either
System.InvalidOperationException: 'The RawStringTranscoder can only decode strings.' https://docs.couchbase.com/dotnet-sdk/current/howtos/transcoders-nonjson.html#rawstringtranscoder
You’re right, it does have the same problem. It appears to have been fixed in the same change that fixed RawBinaryTranscoder.