internal void Serialize(object graph, BinaryFormatterWriter serWriter, bool fCheck)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (serWriter == null)
            {
                throw new ArgumentNullException(nameof(serWriter));
            }

            _serWriter = serWriter;

            serWriter.WriteBegin();
            long   headerId = 0;
            object obj;
            long   objectId;
            bool   isNew;

            // allocations if methodCall or methodResponse and no graph
            _idGenerator        = new ObjectIDGenerator();
            _objectQueue        = new Queue <object>();
            _formatterConverter = new FormatterConverter();
            _serObjectInfoInit  = new SerObjectInfoInit();

            _topId   = InternalGetId(graph, false, null, out isNew);
            headerId = -1;
            WriteSerializedStreamHeader(_topId, headerId);

            _objectQueue.Enqueue(graph);
            while ((obj = GetNext(out objectId)) != null)
            {
                WriteObjectInfo objectInfo = null;

                // GetNext will return either an object or a WriteObjectInfo.
                // A WriteObjectInfo is returned if this object was member of another object
                if (obj is WriteObjectInfo)
                {
                    objectInfo = (WriteObjectInfo)obj;
                }
                else
                {
                    objectInfo          = WriteObjectInfo.Serialize(obj, _surrogates, _context, _serObjectInfoInit, _formatterConverter, this, _binder);
                    objectInfo._assemId = GetAssemblyId(objectInfo);
                }

                objectInfo._objectId = objectId;
                NameInfo typeNameInfo = TypeToNameInfo(objectInfo);
                Write(objectInfo, typeNameInfo, typeNameInfo);
                PutNameInfo(typeNameInfo);
                objectInfo.ObjectEnd();
            }

            serWriter.WriteSerializationHeaderEnd();
            serWriter.WriteEnd();

            // Invoke OnSerialized Event
            _objectManager.RaiseOnSerializedEvent();
        }
Example #2
0
 public void WriteObjectGraph(BinaryWriter writer, object obj, Header[] headers)
 {
     _pendingObjects.Clear();
     if (headers != null)
     {
         QueueObject(headers);
     }
     QueueObject(obj);
     WriteQueuedObjects(writer);
     WriteSerializationEnd(writer);
     _manager.RaiseOnSerializedEvent();
 }
Example #3
0
        internal void Serialize(object objGraph, Header[] headers, FormatterTypeStyle typeFormat, FormatterAssemblyStyle assemblyFormat)
        {
            CultureInfo savedCi = CultureInfo.CurrentCulture;

            try {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                Serialize_inner(objGraph, headers, typeFormat, assemblyFormat);
            } finally {
                Thread.CurrentThread.CurrentCulture = savedCi;
            }

            _manager.RaiseOnSerializedEvent();
        }
        internal void Serialize(object objGraph, Header[] headers, FormatterTypeStyle typeFormat, FormatterAssemblyStyle assemblyFormat)
        {
            _typeFormat     = typeFormat;
            _assemblyFormat = assemblyFormat;
            // Create the XmlDocument with the
            // Envelope and Body elements
            _mapper = new SoapTypeMapper(_xmlWriter, assemblyFormat, typeFormat);

            // The root element
            _xmlWriter.WriteStartElement(
                SoapTypeMapper.SoapEnvelopePrefix,
                "Envelope",
                SoapTypeMapper.SoapEnvelopeNamespace);

            // adding namespaces
            _xmlWriter.WriteAttributeString(
                "xmlns",
                "xsi",
                "http://www.w3.org/2000/xmlns/",
                "http://www.w3.org/2001/XMLSchema-instance");

            _xmlWriter.WriteAttributeString(
                "xmlns",
                "xsd",
                "http://www.w3.org/2000/xmlns/",
                XmlSchema.Namespace);

            _xmlWriter.WriteAttributeString(
                "xmlns",
                SoapTypeMapper.SoapEncodingPrefix,
                "http://www.w3.org/2000/xmlns/",
                SoapTypeMapper.SoapEncodingNamespace);

            _xmlWriter.WriteAttributeString(
                "xmlns",
                SoapTypeMapper.SoapEnvelopePrefix,
                "http://www.w3.org/2000/xmlns/",
                SoapTypeMapper.SoapEnvelopeNamespace);

            _xmlWriter.WriteAttributeString(
                "xmlns",
                "clr",
                "http://www.w3.org/2000/xmlns/",
                SoapServices.XmlNsForClrType);

            _xmlWriter.WriteAttributeString(
                SoapTypeMapper.SoapEnvelopePrefix,
                "encodingStyle",
                SoapTypeMapper.SoapEnvelopeNamespace,
                "http://schemas.xmlsoap.org/soap/encoding/");

            ISoapMessage msg = objGraph as ISoapMessage;

            if (msg != null)
            {
                headers = msg.Headers;
            }

            if (headers != null && headers.Length > 0)
            {
                _xmlWriter.WriteStartElement(SoapTypeMapper.SoapEnvelopePrefix, "Header", SoapTypeMapper.SoapEnvelopeNamespace);
                foreach (Header h in headers)
                {
                    SerializeHeader(h);
                }

                WriteObjectQueue();
                _xmlWriter.WriteEndElement();
            }

            // The body element
            _xmlWriter.WriteStartElement(
                SoapTypeMapper.SoapEnvelopePrefix,
                "Body",
                SoapTypeMapper.SoapEnvelopeNamespace);


            bool firstTime = false;

            if (msg != null)
            {
                SerializeMessage(msg);
            }
            else
            {
                _objectQueue.Enqueue(new EnqueuedObject(objGraph, idGen.GetId(objGraph, out firstTime)));
            }

            WriteObjectQueue();

            _xmlWriter.WriteFullEndElement(); // the body element
            _xmlWriter.WriteFullEndElement(); // the envelope element
            _xmlWriter.Flush();

#if NET_2_0 && !TARGET_JVM
            _manager.RaiseOnSerializedEvent();
#endif
        }