<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Alan Northam : Serialization</title><link>http://devlicio.us/blogs/alan_northam/archive/tags/Serialization/default.aspx</link><description>Tags: Serialization</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>WCF and NHibernate</title><link>http://devlicio.us/blogs/alan_northam/archive/2007/09/25/wcf-and-nhibernate.aspx</link><pubDate>Tue, 25 Sep 2007 20:27:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:38480</guid><dc:creator>anortham</dc:creator><slash:comments>7</slash:comments><description>&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If you&amp;#39;ve tried to pass NHibernate objects over the wire using a webservice before, you know how painful it can be.&amp;nbsp; But what about WCF?&amp;nbsp; Can it ease your pain?&amp;nbsp; Well, yes, of course it can.&amp;nbsp; This wouldn&amp;#39;t be a very interesting post if the answer was no.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; There are three main issues that needed to be fixed:&amp;nbsp; serializing circular references, passing generic list to the client (and not having them converted to arrays), and serializing NHibernate/Castle proxy objects.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Serializing circular references.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; An example of a circular reference would be a User object that has a collection of Roles and each of those Role objects has a collection of User objects.&amp;nbsp; In the past (with webservices) this meant you had to decorate the offending properties with [XMLIgnore] to serialize your objects.&amp;nbsp; This meant that your object graph would be quite different over the wire.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Let&amp;#39;s not dwell on the past.&amp;nbsp; The DataContractSerializer can preserve your object references, but not by default.&amp;nbsp; You&amp;#39;re going to need to extend DataContractSerializerOperationBehavior as described &lt;a href="http://www.jameskovacs.com/blog/CommentView.aspx?guid=477b077c-e65e-4547-8289-4e1bc17b3de7" target="_blank"&gt;here&lt;/a&gt; in an excellent article by &lt;a href="http://www.jameskovacs.com/blog/" target="_blank"&gt;James Kovacs&lt;/a&gt;.&amp;nbsp; The code included with this post is built using his example.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Passing generic collections to the client.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Specifically, I&amp;rsquo;m talking about passing objects that contain collections implemented as IList&amp;lt;T&amp;gt; as is necessary to use generics with NHibernate.&amp;nbsp; This was actually a two part problem.&amp;nbsp; The default serialization in ASP.NET webservices (XMLSerializer) could not serialize an interface and even when casting to List&amp;lt;T&amp;gt; the client would see the collection as an array, not a generic list.&amp;nbsp; In WCF, the DataContractSerializer can serialize IList&amp;lt;T&amp;gt;.&amp;nbsp; Getting the client to see the collection properly requires you to generate your service proxies using the command line utility svcutil.exe (you can&amp;rsquo;t do it using the IDE in VS2005, but you will be able to in VS2008).&amp;nbsp; The command will look something&amp;nbsp;like this (notice the /ct switch, a shortened form of the switch /collectiontype):&lt;/p&gt;
&lt;p&gt;svcutil /language:cs /ct:System.Collections.Generic.List`1&amp;nbsp; /out:AdministrationClient http://localhost:3796/NHBlog.WebUI/Administration.svc&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Serializing NHibernate/Castle proxies.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I&amp;rsquo;m going to save this topic for a separate post.&amp;nbsp; Actually, I&amp;rsquo;m still testing and getting some unexpected results that I need to quantify first.&amp;nbsp; For now, download the sample code included with this post and try it for yourself.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;b&gt;Notes about the sample code.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If this is the first time you are running this app (it&amp;rsquo;s an extension of the sample code from my previous posts) you will need to make sure and add an initial application to the &amp;ldquo;Applications&amp;rdquo; table in the database setting the application name to &amp;ldquo;NHBlog&amp;rdquo;.&amp;nbsp; This is a requirement of the custom MembershipProvider included in the sample.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This code has been used and reused and modified many times.&amp;nbsp; As a result, it may have some rough edges in the presentation but the underlying framework should be solid.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=38480" width="1" height="1"&gt;</description><enclosure url="http://devlicio.us" length="3377785" type="application/x-zip-compressed" /><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/WCF/default.aspx">WCF</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/Serialization/default.aspx">Serialization</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/Featured/default.aspx">Featured</category></item></channel></rss>