Exemple #1
0
        public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            DbConnection conn = null;

            if (context.DependencyCount > 0)
            {
                conn = context.Dependencies[0] as DbConnection;
            }
            var    hasSource = binaryReader.ReadBoolean();
            string source    = "";

            if (hasSource)
            {
                source = binaryReader.ReadString();
            }
            BinaryFormatter    bFormat = new BinaryFormatter();
            ADORecordSetHelper rs      = (ADORecordSetHelper)bFormat.Deserialize(binaryReader.BaseStream);

            rs.ProviderFactory  = AdoFactoryManager.GetFactory("");
            rs.ActiveConnection = conn;
            if (hasSource)
            {
                rs.Source = source;
            }
            return(rs);
        }
Exemple #2
0
        public static object DeserializeDataView(BinaryReader binaryReader, ISurrogateContext context)
        {
            var rowFilter        = binaryReader.ReadString();
            var sort             = binaryReader.ReadString();
            var rowState         = (DataViewRowState)binaryReader.ReadInt32();
            var applyDefaultSort = binaryReader.ReadBoolean();
            var allowDelete      = binaryReader.ReadBoolean();
            var allowEdit        = binaryReader.ReadBoolean();
            var allowNew         = binaryReader.ReadBoolean();

            if (context.Dependencies.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("DataView corrupted " + context.SurrogateUniqueID);
                System.Diagnostics.Debug.WriteLine(new System.Diagnostics.StackTrace().ToString());
            }

            //Gets DataTable reference
            var dataTable = context.Dependencies[0] as DataTable;

            //Creates the DataView instance
            var res = new DataView(dataTable, rowFilter, sort, rowState);

            res.ApplyDefaultSort = applyDefaultSort;
            res.AllowDelete      = allowDelete;
            res.AllowEdit        = allowEdit;
            res.AllowNew         = allowNew;

            return(res);
        }
Exemple #3
0
        public static object RawToObject(object raw, ISurrogateContext context)
        {
            byte[] rawBytes = (byte[])raw;
            lock (_syncSurrogates)
            {
                using (var ms = new MemoryStream(rawBytes))
                {
                    using (var reader = new BinaryReader(ms))
                    {
                        var            signature = reader.ReadString();
                        SurrogatesInfo info      = null;

                        if (SignatureToSurrogate.TryGetValue(signature, out info))
                        {
                            var comparer = "";
                            if (info.WriteComparer != null)
                            {
                                comparer = reader.ReadString();
                            }
                            //Read Dependencies count
                            int count = reader.ReadInt32();
                            //Iterate restoring dependencies
                            for (int i = 0; i < count; i++)
                            {
                                context.RestoreDependency(reader.ReadString(), comparer, info.IsValidDependency);
                            }
                            return(info.DeSerializeEx(reader, context));
                        }
                        throw new NotSupportedException();
                    }
                }
            }
        }
        public static object DeserializeDataTable(BinaryReader binaryReader, ISurrogateContext context)
        {
            var       dataSetSurrogateId = binaryReader.ReadString();
            DataTable dataTable          = null;

            dataTable = BinaryFastSerializer.DeserializeDataTable(context, binaryReader.BaseStream) as DataTable;

            if (context.DependencyCount != 0)
            {
                foreach (var dependency in context.Dependencies)
                {
                    var dataSet = dependency as DataSet;
                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Contains(dataTable.TableName) && dataSet.Relations.Count == 0)
                        {
                            dataSet.Tables.Remove(dataTable.TableName);
                            dataSet.Tables.Add(dataTable);
                        }
                        else if (!dataSet.Tables.Contains(dataTable.TableName))
                        {
                            dataSet.Tables.Add(dataTable);
                        }
                    }
                    else
                    {
                        DeserializeDataTableEvents(context, dataTable, dependency);
                    }
                }
            }

            return(dataTable);
        }
        public object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            string fieldName           = null;
            var    delegateHiddenClass = Activator.CreateInstance(_oType);

            PromiseUtils.DeserializeStateForDelegate(binaryReader, _oType, delegateHiddenClass, ref fieldName);
            return(delegateHiddenClass);
        }
Exemple #6
0
        public static object DeSerializeXmlElement(BinaryReader binaryReader, ISurrogateContext context)
        {
            var doc = context.Dependencies[0] as System.Xml.XmlDocument;

            if (doc == null)
            {
                doc = new XmlDocument();
                ReturnDummy(doc, " doc is null");
            }
            var    isFullXpath = binaryReader.ReadBoolean();
            string stringXml   = String.Empty;
            var    xpath       = binaryReader.ReadString();

            if (!isFullXpath)
            {
                stringXml = binaryReader.ReadString();
            }

            if (isFullXpath)
            {
                //The document is attached to the document.
                var node = doc.SelectSingleNode(xpath);
                if (node == null)
                {
                    return(ReturnDummy(doc, xpath));
                }
                return(node);
            }
            else
            {
                //In the case a node is not attached to a document (any node in its hierarchy has a null parent)
                //the node is serialized by itself independently from the document.
                if (!String.IsNullOrWhiteSpace(stringXml))
                {
                    doc = new XmlDocument();
                    doc.LoadXml(stringXml);
                    if (String.IsNullOrWhiteSpace(xpath))
                    {
                        return(doc.ChildNodes[0]);
                    }
                    else
                    {
                        var node = doc.SelectSingleNode(xpath);
                        if (node == null)
                        {
                            return(ReturnDummy(doc, xpath));
                        }
                        return(node);
                    }
                }
            }
            return(ReturnDummy(doc, xpath));
        }
        public object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var declaringTypeAsString = binaryReader.ReadString();
            var declaringType         = TypeCacheUtils.GetType(declaringTypeAsString);
            var methodName            = binaryReader.ReadString();

            var target = context.Dependencies[0];

            var methodInfo = declaringType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            return(PromiseUtils.CreateDelegateFromMethodInfo(target, methodInfo));
        }
        public static object DeSerializeXmlNode(BinaryReader binaryReader, ISurrogateContext context)
        {
            string xml = binaryReader.ReadString();

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

            //, MOBILIZE, 09/8/2016,TODO,mvega,"Manually changed", "Support case when xml document has no document loaded (xml is empty), to avoid LoadXml throwing an exception"
            if (!string.IsNullOrEmpty(xml))
            {
                doc.LoadXml(xml);
            }
            return(doc);
        }
Exemple #9
0
        public static object DeserializeDataRowView(BinaryReader binaryReader, ISurrogateContext context)
        {
            DataView dataView = context.Dependencies[0] as DataView;
            DataRow  dataRow  = context.Dependencies[1] as DataRow;

            if (dataView == null || dataRow == null)
            {
                throw new ArgumentException();
            }
            object res = constructorDataViewRow.Invoke(new object[] { dataView, dataRow });

            return(res);
        }
Exemple #10
0
        public static object ObjectToRaw(object obj, ISurrogateContext context)
        {
            lock (_syncSurrogates)
            {
                SurrogatesInfo info = null;
                if (obj != null && TypeToSurrogate.TryGetValue(obj.GetType(), out info))
                {
                    object rawObject = null;
                    using (var ms = new MemoryStream())
                    {
                        var binaryWriter = new BinaryWriter(ms);
                        //The surrogate has registered dependencies
                        binaryWriter.Write(info.Signature);
                        if (info.WriteComparer != null)
                        {
                            info.WriteComparer(binaryWriter, obj);
                        }
                        if (info.CalculateDependencies != null)
                        {
                            var count = context.DependencyCount;
                            binaryWriter.Write(count);
                            if (count != 0)
                            {
                                //Writes the dependencies ids
                                context.WriteDependencies((uniqueId) => { binaryWriter.Write(uniqueId); });
                            }
                        }
                        else
                        {
                            //No Dependencies. Just write 0
                            binaryWriter.Write(0);
                        }
                        info.SerializeEx(obj, binaryWriter, ms, context);
                        rawObject = ms.ToArray();
                    }

                    return(rawObject);
                }
                else
                {
                    if (obj != null)
                    {
                        Trace.TraceError("No surrogate registered for type " + obj.GetType().FullName);
                        Trace.TraceError("This value will be lost in following requests");
                    }
                }
                return(new object());//throw new NotSupportedException();//, MOBILIZE,9/4/2015,TODO,KMM,”Temporary code”, “Details related to ViewModels”
            }
        }
        public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var providerName = binaryReader.ReadString();
            var factory      = DbProviderFactories.GetFactory(providerName);
            var conn         = factory.CreateConnection();
            var connString   = binaryReader.ReadString();
            var state        = (System.Data.ConnectionState)binaryReader.ReadInt32();

            conn.ConnectionString = connString;
            if (state == System.Data.ConnectionState.Open)
            {
                conn.Open();
            }
            return(conn);
        }
Exemple #12
0
        public static object DeSerializeXmlNode(BinaryReader binaryReader, ISurrogateContext context)
        {
            var xpath = binaryReader.ReadString();
            var doc   = context.Dependencies[0] as XmlDocument;
            var node  = doc.SelectSingleNode(xpath);

            if (node != null)
            {
                return(doc.SelectSingleNode(xpath));
            }
            else
            {
                System.Diagnostics.Trace.TraceError("SerializeXmlNode element not found by xpath. Returning dummy " + xpath);
                return(doc.CreateElement("DummyXmlSurrogateErrorSerializeXmlNode"));
            }
        }
        public static void DeserializeDataTableEvents(ISurrogateContext context, DataTable dataTable, object dependency)
        {
            var eventInfo = dependency as ISurrogateEventsInfo;

            //Hook up the events delegates.
            switch (eventInfo.EventName.ToLower())
            {
            case "rowdeleting":
            case "rowchanging":
                context.SubscribeEvent <DataRowChangeEventHandler>(eventInfo.EventId, eventInfo.EventName, dataTable);
                break;

            case "tableclearing":
                context.SubscribeEvent <DataTableClearEventHandler>(eventInfo.EventId, eventInfo.EventName, dataTable);
                break;
            }
        }
Exemple #14
0
        public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var surrogate = new StateObjectSurrogate
            {
                UniqueID = context.SurrogateUniqueID,
            };
            var refCount = binaryReader.ReadInt32();
            ///This cast is assumed to be valid because we are inside the v2 implementation assembly
            var surrogateManager = ((UpgradeHelpers.WebMap.Server.Surrogates.SurrogateContext)context)._surrogateManager;

            for (int i = 0; i < refCount; i++)
            {
                var referenceId = binaryReader.ReadString();
                surrogateManager.AddSurrogateReference(surrogate, referenceId);
            }
            return(surrogate);
        }
 private static void WriteValueAux(JsonWriter writer, object item, Type prop, ISurrogateContext context)
 {
     if (prop.IsClass && !prop.IsValueType && !prop.Name.Equals("String"))
     {
         if (item != null)
         {
             WriteObject(writer, item, context);
         }
         else
         {
             writer.WriteValue(String.Empty);
         }
     }
     else
     {
         writer.WriteValue(item);
     }
 }
Exemple #16
0
        public static object DeserializeDataRow(BinaryReader binaryReader, ISurrogateContext context)
        {
            DataRow res = null;

            var rowIndex = binaryReader.ReadInt32();
            var rowID    = binaryReader.ReadInt32();
            var rowError = binaryReader.ReadString();

            rowError = rowError == "null" ? null : rowError;



            var dataTable = (DataTable)context.Dependencies[0];

            try
            {
                //If the data table has been cleared a dummy row is return instead
                //To-do: Implement a DataTable Surrogate in order to ensure rows consistency
                if (dataTable.Rows.Count > 0 && rowIndex != -1)
                {
                    //Lets try to find the Row in the table, if it's still there...
                    res = SurrogateUtils.GetRowInDataTable(dataTable, rowIndex, rowID);
                    if (res == null)
                    {
                        //If the table has no rows then current surrogate is not valid and must be removed as reference for the Table
                        res = dataTable.NewRow();
                        context.RemoveDependency(dataTable);
                    }
                }
                else
                {
                    //If the table has no rows then current surrogate is not valid and must be removed as reference for the Table
                    res = dataTable.NewRow();
                    context.RemoveDependency(dataTable);
                }
            }
            catch
            {
                //Temporary change: ensure that there is always a row to be returned
                res = dataTable.NewRow();
                context.RemoveDependency(dataTable);
            }
            return(res);
        }
    public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
    {
        var connString = binaryReader.ReadString();
        var commandReflectionTypeName = binaryReader.ReadString();

        var          providerName       = binaryReader.ReadString();
        var          commandText        = binaryReader.ReadString();
        int          commandType        = binaryReader.ReadInt32();
        var          connectionUniqueId = binaryReader.ReadString();
        DbConnection conn = null;

        conn = context.Dependencies[0] as DbConnection;
        var factory = DbProviderFactories.GetFactory(providerName);
        var cmd     = factory.CreateCommand();

        cmd.Connection  = conn;
        cmd.CommandText = commandText;
        cmd.CommandType = (System.Data.CommandType)commandType;
        return(cmd);
    }
Exemple #18
0
        public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var dataBinding = new DataBinding();

            dataBinding.SetDataSourceReference(context.Dependencies[0]);
            //DataSourceProperty
            dataBinding.DataSourceProperty = binaryReader.ReadString();
            //Object
            dataBinding.ObjReference = context.Dependencies[1] as IStateObject;
            //Next
            var next = binaryReader.ReadString();

            dataBinding.NextBinding = next == "" ? null : next;
            //Previous
            var previous = binaryReader.ReadString();

            dataBinding.PreviousBinding = previous == "" ? null : previous;
            //ObjectProperty
            dataBinding.ObjProperty = binaryReader.ReadString();
            return(dataBinding);
        }
        private static void SerializeDictionary(JsonWriter writer, object obj, ISurrogateContext context)
        {
            var dict = obj as System.Collections.IDictionary;

            writer.WritePropertyName("__dict");
            writer.WriteStartObject();
            writer.WritePropertyName("_keys");
            writer.WriteStartArray();
            foreach (var key in dict.Keys)
            {
                writer.WriteValue(key);
            }
            writer.WriteEndArray();
            writer.WritePropertyName("_values");
            writer.WriteStartArray();
            foreach (var value in dict.Values)
            {
                var _valueType = value.GetType();
                WriteValueAux(writer, value, _valueType, context);
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
        }
        public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var res   = new System.Collections.SortedList();
            var count = binaryReader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                //Key
                var    isStateObject = binaryReader.ReadBoolean();
                var    data          = binaryReader.ReadString();
                object key;
                if (isStateObject)
                {
                    key = context.RestoreStateObject(data);
                }
                else
                {
                    key = data;
                }
                //Value

                isStateObject = binaryReader.ReadBoolean();
                data          = binaryReader.ReadString();
                object value;
                if (isStateObject)
                {
                    value = context.RestoreStateObject(data);
                }
                else
                {
                    value = data;
                }
                res.Add(key, value);
            }
            return(res);
        }
Exemple #21
0
        public static void SerializeDataRow(object obj, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context)
        {
            var dataRow   = obj as DataRow;
            var dataTable = dataRow.Table;
            var rowIndex  = dataRow.Table.Rows.IndexOf(dataRow);
            var rowError  = dataRow.RowError == null ? "null" : dataRow.RowError;
            var rowID     = SurrogateUtils.GetRowID(dataRow);

            binaryWriter.Write(rowIndex);
            binaryWriter.Write(rowID);
            binaryWriter.Write(rowError);
        }
        public static void SerializeXmlDocument(object obj, BinaryWriter writer, MemoryStream ms, ISurrogateContext context)
        {
            var           doc             = obj as System.Xml.XmlDocument;
            var           stringToBeSaved = "";
            StringWriter  sw  = new System.IO.StringWriter();
            XmlTextWriter xtw = new System.Xml.XmlTextWriter(sw);

            xtw.Formatting = Formatting.None;
            doc.WriteContentTo(xtw);
            stringToBeSaved = sw.ToString();
            writer.Write(stringToBeSaved);
        }
Exemple #23
0
        public object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var serializableObj = (ISerializable)bFormat.Deserialize(binaryReader.BaseStream);

            return(serializableObj);
        }
Exemple #24
0
        public void Serialize(object instance, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context)
        {
            var serializableArg = instance as ISerializable;

            if (serializableArg != null)
            {
                //ISerializable serialized
                try
                {
                    bFormat.Serialize(ms, serializableArg);
                }
                catch (Exception ex)
                {
                    if (instance is System.Collections.ICollection)
                    {
                        var msg = string.Format("Error persisting Surrogate by its ISerializable implementation. Error message {0}. This object is a collection object. The error could be caused because the elements in the collection do not implement ISerializable. If the elements in the collection are IDependentModels or IDepedentViewModels consider using the XmlReference object. For more information contact Mobilize.net with klun", ex.Message);
                        System.Diagnostics.Trace.TraceError(msg);
                    }
                    else
                    {
                        var msg = string.Format("Error persisting Surrogate by its ISerializable implementation. Error message {0}.", ex.Message);
                        System.Diagnostics.Trace.TraceError(msg);
                    }
                }
            }
        }
        public static void Serialize(object instance, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context)
        {
            var sortedList = instance as System.Collections.SortedList;

            binaryWriter.Write(sortedList.Count);
            foreach (System.Collections.DictionaryEntry item in sortedList)
            {
                var asStateObject = item.Key as IStateObject;
                //Flag to determine if value is stateobject
                binaryWriter.Write(asStateObject != null);
                //Save Key
                if (asStateObject != null)
                {
                    if (!StateManager.AllBranchesAttached(asStateObject.UniqueID))
                    {
                        var sortedListSurrogate = StateManager.Current.surrogateManager.GetSurrogateFor(sortedList, generateIfNotFound: false);
                        AdoptionInformation.StaticAdopt(sortedListSurrogate, asStateObject);
                    }

                    //TODO this line has to be removed from here when the SaveObject will be generalized
                    StateManager.Current.StorageManager.SaveObject(asStateObject);

                    foreach (var pair in StateManager.Current.GetDependentItemsInCache(asStateObject.UniqueID))
                    {
                        StateManager.Current.StorageManager.SaveObject(pair.Value);
                    }

                    binaryWriter.Write(asStateObject.UniqueID);
                }
                else
                {
                    binaryWriter.Write(item.Key.ToString());
                }
                //Save Value
                asStateObject = item.Value as IStateObject;
                binaryWriter.Write(asStateObject != null);
                //Save Key
                if (asStateObject != null)
                {
                    if (!StateManager.AllBranchesAttached(asStateObject.UniqueID))
                    {
                        var sortedListSurrogate = StateManager.Current.surrogateManager.GetSurrogateFor(sortedList, generateIfNotFound: false);
                        AdoptionInformation.StaticAdopt(sortedListSurrogate, asStateObject);
                    }

                    //TODO this line has to be removed from here when the SaveObject will be generalized
                    StateManager.Current.StorageManager.SaveObject(asStateObject);

                    foreach (var pair in StateManager.Current.GetDependentItemsInCache(asStateObject.UniqueID))
                    {
                        StateManager.Current.StorageManager.SaveObject(pair.Value);
                    }

                    binaryWriter.Write(asStateObject.UniqueID);
                }
                else
                {
                    binaryWriter.Write(item.Key.ToString());
                }
            }
        }
Exemple #26
0
        public static object Deserialize(BinaryReader binaryReader, ISurrogateContext context)
        {
            var assemblyName = binaryReader.ReadString();

            return(Assembly.Load(assemblyName));
        }
        public void Serialize(object instance, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context)
        {
            var delegateObj = ((MulticastDelegate)instance);

            binaryWriter.Write(delegateObj.Method.DeclaringType.AssemblyQualifiedNameCache());
            binaryWriter.Write(delegateObj.Method.Name);
        }
Exemple #28
0
        public static void SerializeXmlNode(object obj, BinaryWriter writer, MemoryStream ms, ISurrogateContext context)
        {
            var node     = obj as System.Xml.XmlNode;
            var document = node.OwnerDocument;
            var path     = String.Empty;

            XmlElementHelper.FindXPath(node, out path);

            writer.Write(path);
        }
    public static void Serialize(object instance, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context)
    {
        var cmd = instance as System.Data.Common.DbCommand;

        if (cmd != null)
        {
            binaryWriter.Write(cmd.GetType().Namespace); //Used for provider
            binaryWriter.Write(cmd.CommandText);
            binaryWriter.Write((Int32)cmd.CommandType);
        }
        throw new ArgumentException("SurrogateSerialize Error: Invalid object type expected System.Data.Common.DbConnection");
    }
Exemple #30
0
        public static void Serialize(object instance, BinaryWriter binaryWriter, MemoryStream ms, ISurrogateContext context)
        {
            var rs = instance as ADORecordSetHelper;
            //ADORecordSet serialized
            var ms_adorecordset = new MemoryStream();
            var source          = rs.Source;

            if (source is DbCommand)
            {
                DbCommand cmd = (DbCommand)source;
                binaryWriter.Write(true);
                binaryWriter.Write(cmd.CommandText);
            }
            else if (source is string)
            {
                binaryWriter.Write(true);
                binaryWriter.Write(source.ToString());
            }
            else
            {
                binaryWriter.Write(false);
            }
            BinaryFormatter bFormat = new BinaryFormatter();

            bFormat.Serialize(ms_adorecordset, rs);
            binaryWriter.Write(ms_adorecordset.ToArray());
        }