JavascriptSerializer and NHibernate proxy's

111 views
Skip to first unread message

Vladan Strigo

unread,
Jan 28, 2010, 9:36:23 AM1/28/10
to nhusers
...doesn't work. It says there are circular references, when in fact
there are none.

Here is my model:

public class Product : Entity
{
public virtual string Name { get; set; }
}

public class Prospect : Entity
{
public virtual string Sex { get; set; }
public virtual string Initials { get; set; }
public virtual string LastName { get; set; }
public virtual int HouseNo { get; set; }
public virtual Product Product { get; set; }
}


As you can see, its a simple one.

When using the JS Serializer on it:

var serialize = (new JavaScriptSerializer()).Serialize(list);


it brakes with "A circular reference was detected while serializing an
object of type "System.Reflection.Module""

As soon as I slim down my model to exclude "Product" entity from being
mapped (so that its a normal child entity), it works ok.

The best thing I can conclude is that its because of Castle's proxy's.


Did anyone tackle this problem? I've seen solutions for JSON.Net, but
as the application on which I am working is working with built in MS
MVC action results (one of them being Json action result)...it
automatically uses that for serialization.

Any ideas?

Vladan

allan....@gmail.com

unread,
Jan 28, 2010, 9:42:38 AM1/28/10
to nhusers
You are correct in thinking that the problem you are having is because
of the lazy loading situation (proxies).
The JavaScriptSerializer that MVC uses by default is a very unflexible
piece of code and does not work well in these scenarios. I would
suggest not using it at all.
I got around this by using JSON.NET and creating a custom action
result.

Vladan Strigo

unread,
Jan 28, 2010, 9:46:42 AM1/28/10
to nhusers
could you give me an example of configuring JSON.net to work correctly
in this scenario?

btw. thnx for your answer! :)


Vladan

On 28 sij, 15:42, "allan.ritc...@gmail.com" <allan.ritc...@gmail.com>
wrote:

Vladan Strigo

unread,
Jan 29, 2010, 4:17:00 AM1/29/10
to nhusers
nevermind...I've been able to hack it up myself!

thanks anyway! :)

Fabio Maulo

unread,
Jan 29, 2010, 7:30:39 AM1/29/10
to nhu...@googlegroups.com
would be nice if you send the solution here... (as a courtesy before your next support request).

2010/1/29 Vladan Strigo <vladan...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Vladan Strigo

unread,
Jan 30, 2010, 4:43:50 AM1/30/10
to nhusers
Np, although I've found a problem with it. It doesn't serialize
correctly (I get wrong data in wrong columns in JSON).

As soon as I find a working solution it will be posted here...thanks
for reminding me Fabio! :)

allan.ritchie...would it be possible to see your solution? maybe it
will save me some pain?


Vladan


On 29 sij, 13:30, Fabio Maulo <fabioma...@gmail.com> wrote:
> would be nice if you send the solution here... (as a courtesy before your
> next support request).
>

> 2010/1/29 Vladan Strigo <vladan.str...@gmail.com>

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages