public void Serialize(T obj, BinaryWriter writer) { var idAndSerializer = SerializersCache.GetOrAdd(Tuple.Create(typeof(T), _environment), type => { var newSerializer = _serializerFactory.BuildSerializer(); var schema = _serializerFactory.GetSchema(); var newSchemaId = _registryApi.Register(_subject, schema).Result; return(Tuple.Create(newSchemaId, (object)newSerializer)); }); var schemaId = idAndSerializer.Item1; var serializer = (Action <Stream, T>)idAndSerializer.Item2; writer.Write(MagicByte); writer.Write(IPAddress.HostToNetworkOrder(schemaId)); serializer(writer.BaseStream, obj); }