A serialization callback does not have the signature the serializer calls, so serializing fails at run time

tx0/csharp:serialization-callback-signaturestrictwarningreliability/object-contractsdepth fastbug

What it finds

A method marked [OnSerializing], [OnSerialized], [OnDeserializing] or [OnDeserialized] is called by the serializer with one StreamingContext; with another signature serialization fails at run time with an exception, in the data contract serializer and in Json.NET alike. The method must return void, take exactly one StreamingContext parameter and have no type parameters. Files that import protobuf-net, which also calls such methods without a context, are left alone.

From the rule's source, serialization_callback_signature.rs.

Example from the rule's tests
using System.Runtime.Serialization; [DataContract]class Order{    [OnDeserialized]    private void Restore()tx0 reports: [OnDeserialized] method Restore takes 0 parameters    {    }     [OnSerializing]    private bool Prepare(StreamingContext context) => true;tx0 reports: [OnSerializing] method Prepare returns bool     [OnSerialized]    private void Done(object context)tx0 reports: [OnSerialized] method Done takes a object    {    }     [OnDeserializing]    private void Start(StreamingContext context)    {    }     [OnDeserialized]    internal void Finish(System.Runtime.Serialization.StreamingContext context)    {    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:serialization-callback-signature" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:serialization-callback-signature] reason="..."

tx0 explain tx0/csharp:serialization-callback-signature shows the same in the terminal. Found a false positive? Report it.