{"record":{"id":"03db3917b7a93e8d","repo":"microsoft/autogen","slug":"message-must-be-a-proto-imessage","errorCode":null,"errorMessage":"Message must be a proto IMessage","messagePattern":"Message must be a proto IMessage","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/ProtobufMessageSerializer.cs","lineNumber":44,"sourceCode":"            var nameOfMethod = nameof(Any.Unpack);\n            var result = message.GetType().GetMethods().Where(m => m.Name == nameOfMethod && m.IsGenericMethod).First().MakeGenericMethod(_concreteType).Invoke(message, null);\n            return result as IMessage ?? throw new ArgumentException(\"Failed to deserialize\", nameof(message));\n        }\n\n        // Raise an exception if the concrete type is not a proto IMessage\n        throw new ArgumentException(\"Concrete type must be a proto IMessage\", nameof(_concreteType));\n    }\n\n    public Any Serialize(object message)\n    {\n        // Check if message is a proto IMessage\n        if (message is IMessage protoMessage)\n        {\n            return Any.Pack(protoMessage);\n        }\n\n        // Raise an exception if the message is not a proto IMessage\n        throw new ArgumentException(\"Message must be a proto IMessage\", nameof(message));\n    }\n}\n","sourceCodeStart":26,"sourceCodeEnd":47,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/ProtobufMessageSerializer.cs#L26-L47","documentation":"Thrown by ProtobufMessageSerializer.Serialize when the object passed in does not implement Google.Protobuf.IMessage. Serialization is done via Any.Pack(protoMessage), which requires a protobuf message instance; any other CLR object is rejected.","triggerScenarios":"Calling Serialize with a POCO, anonymous object, string, or JSON-serialized byte payload instead of a protobuf message; a generic publish path routing non-proto messages into the protobuf serializer.","commonSituations":"Publishing domain DTOs through a runtime configured only with the protobuf serializer; migration from JSON-based messages to protobuf where some call sites still send POCOs.","solutions":["Pass protobuf-generated message instances to Serialize/publish","Register a serializer appropriate to the message kind (e.g. JSON serializer for POCOs) so the registry picks the right one","Add a compile-time constraint or generic guard so non-proto messages cannot reach the protobuf path"],"exampleFix":"// before\nserializer.Serialize(new MyDto { Id = 1 });\n\n// after\nserializer.Serialize(new MyProtoMessage { Id = 1 });","handlingStrategy":"type-guard","validationCode":"if (message is not Google.Protobuf.IMessage) throw new ArgumentException(\"Use a JSON serializer for non-proto messages\");","typeGuard":"static bool IsProtobufMessage(object msg) => msg is Google.Protobuf.IMessage;","tryCatchPattern":"try { return serializer.Serialize(message); } catch (ArgumentException ex) when (ex.Message.Contains(\"must be a proto IMessage\")) { return jsonSerializer.Serialize(message); }","preventionTips":["Route messages by type before choosing a serializer","Adhere to one message encoding per topic"],"tags":["protobuf","serialization","type-validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}