1
votes

I have a custom subclass that needs to be serialized and stored in Isolated StorageSettings(wp7).I have subclass that needs to be serialized and that class extends its base class So i am serializing both classes,When serializing the classes,I am using [DataContract] to classname and [DataMember] to the variables,meanwhile i made all the variables public ,when trying to save the data in IsolatedStorageSettings i got the exception like

The data contract type XXXx is not serializable because it is not public. Making the type public will fix this error.

and so i googled and went to concept of adding assembly like

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]

in my class files and finally data objects are getting saved,but when deactivating the app,data gets saved without error and when activating the app,i am getting the object ,but the variables are null state in the object that i restored back,I want to get the variables and its values.Am i missing anything that causes variables in my saved Object as null?

Code i am updating below

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Phone.Controls;

using System.Diagnostics;
using System.Runtime.Serialization;
using System.ComponentModel;

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]

namespace MyApp.Data
{

    [DataContract]
    public class MyDetail : MyDataSet,INotifyPropertyChanged
    {


        public MyDetail (String data)
        {

            ParseResponse(data);
        }

        public MiniStatementDetail(PhoneApplicationPage context)
        {
            _context = context;
        }
        public void performServerRequest(String mobno, String custno, String uid, String AccNo)
        {

            myProtocol request = this.getProtocol(4);               //Pass Service ID
            request.Parameters.append("mobno", mobno);
            request.Parameters.append("custno", custno);
            request.Parameters.append("uid", uid);
            request.Parameters.append("AccNo", AccNo);

            request.Submit();
        }

        public override void ResponseReceived(String response)
        {
           //         
            MyDetail data = new MyDetail (response);
            ResponseReceived(data);
        }



    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(
                this,
                new PropertyChangedEventArgs(propName));
        }
    }
    }
}

and the subclass I have added below

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using MyApp.Lib;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using MyApp;

using System.Xml.Serialization;//--Added for testing Purpose -Rakesh (10/4/2012)
using System.Runtime.Serialization;
using System.ComponentModel;//--Added for testing Purpose -Rakesh (10/4/2012)

using MyApp.config;

//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization")]

namespace MyApp.data
{
    [DataContract]//--Added for testing Purpose -Rakesh (10/4/2012)
    public class MyDataSet : INotifyPropertyChanged
    {

        [DataMember]
        public List<String[]> _data;
        [DataMember]
        public String[] headers;
        [DataMember]
        public String[] headersAr;
        [DataMember]
        public NameValueCollection _params;
        [DataMember]
        public NameValuePair _params1;
        [DataMember]
        public String _status;
        [DataMember]
        public String _refno;
        [DataMember]
        public String _ptoken;
        [DataMember]
        public String _acid;
        [DataMember]
        public String _branchid;
        [DataMember]
        public String _applicationid;
        [DataMember]
        public String _statusDesc;
        [DataMember]
        public const String HEADER_SEPARATOR = ",";
        [DataMember]
        public const char HEADER_SEPARATOR_SEMI = ';';
        [DataMember]
        public const String ROW_SEPARATOR = "@";

        public event EventHandler OnRequestCompleted;
        [DataMember]
        public PhoneApplicationPage _context;
        [DataMember]
        public NameValueCollection _servicevalues;
        [DataMember]
        public List<String[]> parseddemo;


        public List<String[]> getDataRows()
        {
            return _data;
        }

        public String[] getDataRow(int index)
        {
            return (String[])_data[index];
        }

        public String getToken() { return _ptoken; }

        public String getStatus() { return _status; }


        public String getRefNo() { return _refno; }


        public String[] getHeaders() { return headers; }


        public String getAccountID() { return _acid; }

        public String getApplicationID() { return _applicationid; }

        public String getStatusDesc() { return _statusDesc; }


        public String getHeader(int headerConstant)
        {
            return headers[headerConstant];
        }

        public void ParseResponse(String response)
        {
            App.endResponse = response;
            Debug.WriteLine("Parse Response >>>" + response + "::>>::");
             if (_status.Equals("200"))
            {
                //Common.DisplayAlert(_params.getValueByName("error"));
                LoggedUser.doLogout();
                _context.NavigationService.Navigate(new Uri("/Design/LoginLanding.xaml", UriKind.RelativeOrAbsolute));
                return;
            }
            _statusDesc = _params.getValueByName("StatusDesc");
            _refno = _params.getValueByName("refno");
            _ptoken = _params.getValueByName("ptoken");
            _acid = _params.getValueByName("acid");
            _branchid = _params.getValueByName("branchid");
            _applicationid = _params.getValueByName("ApplicationId");
            _data = ParseResponse(response, "Stmt");



        }

        public List<String[]> ParseResponse(String response, String param)
        {
            List<String[]> data = new List<String[]>();
            String paramValue = _params.getValueByName(param);
            if (!String.IsNullOrEmpty(paramValue))
            {
                String[] dataRows = paramValue.Split(ROW_SEPARATOR.ToCharArray()[0]);
                for (int i = 0; i < dataRows.Length; i++)
                {
                    data.Add(dataRows[i].Split(';'));
                }
            }
            return data;
        }






        public List<String[]> getParamValueBasedData(String paramValue)
        {
            List<String[]> data = new List<String[]>();
            if (paramValue == null) paramValue = "";
            if (!String.IsNullOrEmpty(paramValue))
            {
                String[] dataRows = paramValue.Split(ROW_SEPARATOR.ToCharArray()[0]);
                for (int i = 0; i < dataRows.Length; i++)
                {
                    data.Add(dataRows[i].Split(';'));
                }
            }
            return data;
        }

        public String[] getColumArray(int colid, String firstOption)
        {
            return getColumArray(_data, colid, firstOption);
        }

        public String[] getColumArray(int colid)
        {
            return getColumArray(_data, colid);
        }

        public String[] getColumArray(List<String[]> data, int colid)
        {
            String[] retValues = new String[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                retValues[i] = ((String[])data[i])[colid];
            }
            return retValues;
        }

        public String[] getColumArray(List<String[]> data, int colid, String firstOption)
        {
            String[] retValues = new String[data.Count + 1];
            retValues[0] = firstOption;
            for (int i = 0; i < data.Count; i++)
            {
                retValues[i + 1] = ((String[])data[i])[colid];
            }
            return retValues;
        }

        public String getParam(String name)
        {
            return _params.getValueByName(name);
        }

        public void OnServerResponse(object sender, EventArgs e)
        {
            myProtocol request = (myProtocol)sender;
            Debug.WriteLine("OnServerResponse::MBDataSet:" + request.Response);
            ResponseReceived(request.Response);
        }

        public myProtocol getmyProtocol(int serviceId)
        {
            myProtocol request = new myProtocol(serviceId);
            request.OnRequestCompleted += new EventHandler(OnServerResponse);
            return request;
        }

        public virtual void ResponseReceived(String response)
        {
        }

        {
            if (OnRequestCompleted != null)
            {
                OnRequestCompleted.Invoke(data, EventArgs.Empty);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(
                    this,
                    new PropertyChangedEventArgs(propName));
            }
        }
    }

}

and forgot mention saving and restoring process.i am saving the mycustom Object as

var settings = IsolatedStorageSettings.ApplicationSettings;

            if (settings.Contains("SimpleData"))
            {
                settings["SimpleData"] = requiredObj
            }
            else
            {
                settings.Add("SimpleData", requiredObj);

            }

and i am restoring as gn below

  try
           {
               var settings = IsolatedStorageSettings.ApplicationSettings;
               MyDetail demoObj= null;
               if (settings.Contains("SimpleData"))
               {
                   Debug.WriteLine("Data Present");
                   settings.TryGetValue<MyDetail >("SimpleData", out demoObj)
                   requiredObj= demoobj;

               }
               else
               {
                   Debug.WriteLine("Data Absent");
               }

           }
           catch (Exception ex)
           {
               Debug.WriteLine("Exception while retreving datas" + ex);
           }

and the image of issue is below enter image description here

2
Can you post your two classes and how you are where the objects are being serialized and de-serialized? - NexAddo
Without actual code how do you expect us to help you? I will remove my downvote once I see actual C# code. - Security Hound
Hi ,I added full code.Am i missinganything that causes issues? - Rakesh
So you hit the "Data Present" line, but in the end the requiredObj is null? - Jevgeni Tsaikin
obj is not null,but the variables of the object such as params which was in MyDataSet has been null.Am i making it clear? - Rakesh

2 Answers

0
votes

Have you seen the following "Serialization Guidelines"? http://msdn.microsoft.com/en-us/library/6exf3h2k.aspx

Is the class and the properties specified as public? it contains: "1.CONSIDER marking data members of your type public if the type can be used in partial trust. In full trust, data contract serializers can serialize and deserialize nonpublic types and members, but only public members can be serialized and deserialized in partial trust."

Otherwise paste the code and we might find an answer. Also it would be the best to DEBUG and check if you really store and get some data from IsolatedStorage?

0
votes

To anyone who can help (see SecurityException when serializing with DataContractSerializer and http://systemmetaphor.blogspot.fr/2010/04/silverlight-serialization-avoiding.html)

[assembly: InternalsVisibleTo("System.Runtime.Serialization, PublicKey="
+ "00240000048000009400000006020000002400005253413100040000010001008D56C76F9E86493"
+ "83049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E"
+ "9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4"
+ "BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B"
+ "37AB")]
[assembly: InternalsVisibleTo("System.ServiceModel.Web, PublicKey="
+ "00240000048000009400000006020000002400005253413100040000010001008D56C76F9E86493"
+ "83049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E"
+ "9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4"
+ "BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B"
+ "37AB")]
[assembly: InternalsVisibleTo("System.Runtime.Serialization.Json, PublicKey="
+ "00240000048000009400000006020000002400005253413100040000010001008D56C76F9E86493"
+ "83049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E"
+ "9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4"
+ "BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B"
+ "37AB")]