Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docfx/docs/custom-converters.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ Modify state on an existing @Nerdbank.MessagePack.MessagePackSerializer by captu

[!code-csharp[](../../samples/cs/CustomConverters.cs#ModifyStateOnSerializer)]

#### Per-call state

When the state varies from one call to the next (e.g. per RPC message rather than per connection), creating a new @Nerdbank.MessagePack.MessagePackSerializer for each call is unnecessary.
Comment thread
Copilot marked this conversation as resolved.
Resolved
Instead, pass a <xref:Nerdbank.MessagePack.SerializationContext> directly to one of the overloads that accepts one, such as <xref:Nerdbank.MessagePack.MessagePackSerializer.Serialize``1(Nerdbank.MessagePack.MessagePackWriter@,``0@,PolyType.ITypeShape{``0},Nerdbank.MessagePack.SerializationContext)>.
These overloads use the supplied context instead of <xref:Nerdbank.MessagePack.MessagePackSerializer.StartingContext>, and take their cancellation token from its <xref:Nerdbank.MessagePack.SerializationContext.CancellationToken> property.
The context is typically derived from <xref:Nerdbank.MessagePack.MessagePackSerializer.StartingContext> so that it retains any other settings on it:

[!code-csharp[](../../samples/cs/CustomConverters.cs#PerCallState)]

These overloads are available for <xref:Nerdbank.MessagePack.MessagePackWriter>, <xref:Nerdbank.MessagePack.MessagePackReader>, <xref:System.IO.Pipelines.PipeWriter> and <xref:System.IO.Pipelines.PipeReader>, for both generic and untyped (`*Object`) (de)serialization.
To (de)serialize with other inputs or outputs (e.g. a `byte[]` or <xref:System.IO.Stream>), wrap them in one of these types first.

A converter must never pass its own <xref:Nerdbank.MessagePack.SerializationContext> to these top-level methods.
Doing so throws an <xref:System.ArgumentException>.
Use <xref:Nerdbank.MessagePack.SerializationContext.GetConverter*> to (de)serialize nested values instead.

### Async converters

@Nerdbank.MessagePack.MessagePackConverter`1 is an abstract class that requires a derived converter to implement synchronous @Nerdbank.MessagePack.MessagePackConverter`1.Write* and @Nerdbank.MessagePack.MessagePackConverter`1.Read* methods.
Expand Down
15 changes: 15 additions & 0 deletions samples/cs/CustomConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,21 @@ MessagePackSerializer ModifySerializer(MessagePackSerializer serializer)
return serializer;
}
}

class PerCallState
{
void Serialize(MessagePackSerializer serializer, System.Buffers.IBufferWriter<byte> bufferWriter, SpecialType value, CancellationToken cancellationToken)
{
#region PerCallState
SerializationContext context = serializer.StartingContext with { CancellationToken = cancellationToken };
context["ValueMultiplier"] = 5;

MessagePackWriter writer = new(bufferWriter);
serializer.Serialize(ref writer, value, TypeShapeResolver.ResolveDynamicOrThrow<SpecialType>(), context);
writer.Flush();
#endregion
}
}
}

namespace CustomConverterFactory
Expand Down
Loading
Loading