Serialization Issues and bloated objects

G

Guest

Hi,
I am using the ASP.net Profile frameowrk to provide functinalitty for my site.
One of the object I am serializing is a class I created called Member
containing some integer, datetime and string members. In addition it contains
a typed list of other type of object called Policy with typically 3-4 items.
When the object gets serialzed the resulting database entry has 100s of
Policy objects, way more than the original object included. Many of the same
items just get duplicated again and again whic results in huge database
entries that slow my applicaiton to a halt.

Some relevant code samples:
The Policies class:

[Serializable]
public class Policies : List<Policy>
{
public Policies(IDataReader rdr)
{
while (rdr.Read())
{

this.Add(new Policy(rdr));

}
}
public Policies() { }
public Policy GetById(int Id)
{
foreach (Policy pol in this)
{
if (pol.PolicyID == Id)
{
return pol;
}
}
throw new Exception("Policy Not Found");


}

}


Any ideas?

Thanks in advance





---
Sagi Shkedy
Lead Developer
DMi Partners
Visit My technical Blog at:
http://blog.shkedy.com
 
W

Walter Wang [MSFT]

Hi Sagi,

Are you referring to the default profile provider that is using SqlExpress
as the database store? If this is the case, based on my test, the saved
personal information using profile provider will be all contained in one
table aspnet_Profile and there's only one record for one user.

Here's my test steps:

1) Create following classes:

namespace myns
{
[Serializable]
public class Member
{
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;

public int Age
{
get { return _age; }
set { _age = value; }
}

private Policies _policies;

public Policies Policies
{
get { return _policies; }
set { _policies = value; }
}

}

[Serializable]
public class Policy
{
private int _id;

public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}

public Policy()
{
}

public Policy(int id, string name)
{
_id = id;
_name = name;
}
}

[Serializable]
public class Policies : List<Policy>
{
}
}


2) Edit web.config to support profile:

<profile>
<properties>
<add name="Name" type="string"/>
<add name="Member" type="myns.Member"/>
</properties>
</profile>

3) In Default.aspx.cs

if (string.IsNullOrEmpty(Profile.Name))
{
Profile.Name = "Test";
}

if (Profile.Member.Policies == null)
{
Profile.Member.Age = 1;
Profile.Member.Name = "Name";
Profile.Member.Policies = new Policies();
Profile.Member.Policies.Add(new Policy(1, "n1"));
Profile.Member.Policies.Add(new Policy(2, "n2"));
}

4) Open App_Data\AspNetDb.mdf, view table aspnet_Profile's data. It only
contains one record for the user. The field PropertyNames has following
content: "Name:S:0:4:Member:S:4:365:" while there're other two fields used
to store the serialized value:

Test<?xml version="1.0" encoding="utf-16"?>
<Member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Name</Name>
<Age>1</Age>
<Policies>
<Policy>
<Id>1</Id>
<Name>n1</Name>
</Policy>
<Policy>
<Id>2</Id>
<Name>n2</Name>
</Policy>
</Policies>
</Member>


I think you may referring to other profile provider. Please let me know
your usage scenario. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Shkedy

Right - the default profile provider. The problem was with the serilization
of a list object that is nested inside another object.
If I Look into the serialized object the list elements are duplicated which
cause the object to be really big. I am attaching a section of the
serialized text and the list objects keep repeating:

<Member>
<policies>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>18</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>124</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>19</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-08-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
<Policy>
<MemberID>2014918</MemberID>
<PolicyID>20</PolicyID>
<EffectiveDate>2006-12-16T00:00:00</EffectiveDate>
<TerminationDate>2007-03-16T00:00:00</TerminationDate>
<ServiceID>125</ServiceID>
</Policy>
.....
That's the problem


Walter Wang said:
Hi Sagi,

Are you referring to the default profile provider that is using SqlExpress
as the database store? If this is the case, based on my test, the saved
personal information using profile provider will be all contained in one
table aspnet_Profile and there's only one record for one user.

Here's my test steps:

1) Create following classes:

namespace myns
{
[Serializable]
public class Member
{
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;

public int Age
{
get { return _age; }
set { _age = value; }
}

private Policies _policies;

public Policies Policies
{
get { return _policies; }
set { _policies = value; }
}

}

[Serializable]
public class Policy
{
private int _id;

public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}

public Policy()
{
}

public Policy(int id, string name)
{
_id = id;
_name = name;
}
}

[Serializable]
public class Policies : List<Policy>
{
}
}


2) Edit web.config to support profile:

<profile>
<properties>
<add name="Name" type="string"/>
<add name="Member" type="myns.Member"/>
</properties>
</profile>

3) In Default.aspx.cs

if (string.IsNullOrEmpty(Profile.Name))
{
Profile.Name = "Test";
}

if (Profile.Member.Policies == null)
{
Profile.Member.Age = 1;
Profile.Member.Name = "Name";
Profile.Member.Policies = new Policies();
Profile.Member.Policies.Add(new Policy(1, "n1"));
Profile.Member.Policies.Add(new Policy(2, "n2"));
}

4) Open App_Data\AspNetDb.mdf, view table aspnet_Profile's data. It only
contains one record for the user. The field PropertyNames has following
content: "Name:S:0:4:Member:S:4:365:" while there're other two fields used
to store the serialized value:

Test<?xml version="1.0" encoding="utf-16"?>
<Member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Name</Name>
<Age>1</Age>
<Policies>
<Policy>
<Id>1</Id>
<Name>n1</Name>
</Policy>
<Policy>
<Id>2</Id>
<Name>n2</Name>
</Policy>
</Policies>
</Member>


I think you may referring to other profile provider. Please let me know
your usage scenario. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
W

Walter Wang [MSFT]

Hi Sagi,

I see.

Let's take following code for example:

[Serializable]
public class Member
{
private Policies _policies = new Policies();

(I have the policies collection created by default).

Then I have following code in Page_Load():

Profile.Member.Age = 1;
Profile.Member.Name = "Name";
Profile.Member.Policies.Add(new Policy(1, "n1"));
Profile.Member.Policies.Add(new Policy(2, "n2"));


Refreshing the page several times, and you will see duplicated policy
objects in the serialized text. This is because I'm not checking if the
policies collection is initialized or not before adding new policy objects.
Note that since Profile has already saved the object for us, when we
execute the code second time, there're already policies objects there.

If in doubt, please post some of your code how you're reading/writing the
profile. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Shkedy

I think it is working now but I am still not sure what was causing it.
The way I had it in my class was different. I will keeo this in mind, thanks
for your help.
Sagi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top