{"record":{"id":"af1947ab53777aa5","repo":"egametang/ET","slug":"serialize-error-message-gettype-fullname-e","errorCode":null,"errorMessage":"Serialize error {message.GetType().FullName}\n{e}","messagePattern":"Serialize error (.+?)\n(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs","lineNumber":115,"sourceCode":"        {\n            try\n            {\n                if (message is ISupportInitialize supportInitialize)\n                {\n                    supportInitialize.BeginInit();\n                }\n\n                using BsonBinaryWriter bsonWriter = new(stream, BsonBinaryWriterSettings.Defaults);\n            \n                BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);\n                BsonSerializationArgs args = default;\n                args.NominalType = typeof (object);\n                IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);\n                serializer.Serialize(context, args, message);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"Serialize error {message.GetType().FullName}\\n{e}\");\n            }\n        }\n\n        public static object Deserialize(Type type, byte[] bytes)\n        {\n            try\n            {\n                return BsonSerializer.Deserialize(bytes, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {bytes.Length}\", e);\n            }\n        }\n\n        public static object Deserialize(Type type, byte[] bytes, int index, int count)\n        {\n            try","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L97-L133","documentation":"Thrown by MongoHelper.Serialize(object, MemoryStream) (line 115). Unlike error 144 this path serializes with NominalType = typeof(object), forcing discriminator-based polymorphic serialization: the driver must resolve the concrete serializer from the _t discriminator. Any concrete type without a registered serializer/class map fails here. Inner exception is NOT preserved.","triggerScenarios":"Streaming a message whose runtime type has no object-serializer registration, writing to a stream in a bad state, or null message (catch NREs on message.GetType()). Used for length-prefixed network framing.","commonSituations":"ET network message framing (Serialize to MemoryStream then length-prefix), introducing a new message type without registering it, sending across an assembly boundary where the serializer registry differs.","solutions":["Register the concrete message type's serializer/class map (and [BsonKnownTypes] on the base) so typeof(object) nominal lookup resolves it.","Ensure the message type is in an assembly scanned at startup for class maps.","Null-check message before serializing.","Verify the MemoryStream is writable and positioned correctly."],"exampleFix":"// before\nMongoHelper.Serialize(msg, stream);\n\n// after\nif (msg == null) throw new ArgumentNullException(nameof(message));\nif (!stream.CanWrite) throw new InvalidOperationException(\"stream not writable\");\nMongoHelper.Serialize(msg, stream);","handlingStrategy":"validation","validationCode":"if (message == null) throw new ArgumentNullException(nameof(message));\nif (!BsonSerializer.IsTypeRegistered(message.GetType()))\n    throw new InvalidOperationException($\"no bson serializer for {message.GetType().FullName}\");","typeGuard":null,"tryCatchPattern":"try { MongoHelper.Serialize(message, stream); }\ncatch (Exception e) { Log.Error($\"stream Serialize {message?.GetType()} failed: {e}\"); throw; }","preventionTips":["Register class maps for every network message type at startup.","Never pass null message (catch NREs on GetType).","Confirm the target stream is writable."],"tags":["serialization","mongodb","bson","network"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}