Search code examples
nhibernatesoapxml-serializationlazy-evaluationproxy-classes

using Nhibernate lazy proxys with a webservice


Recently i had some performance problems in a SOAP webservice I wrote a while ago. I noticed I had a lot of queries going on and my hbm.xml mappings where full of lazy=false statements. I upgraded to NHibernate 3.0 and removed the lazy = false stuff and everything was a LOT faster....but now i am getting the following error:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type UserProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

User is a class of which i removed the lazy=false property from the class tag like this:

<class name="User" table="Users" >
  <id name="DatabaseID" unsaved-value="0" column="ID" type="integer" >
    <generator class="native"/>
   </id>
   <property name="IsExpert"/>
    .....more stuff here....
</class>

My webservice has a method like this (simplified a little..in real-life i use a repository-like pattern between the service and nhibernate):

    [WebMethod]
    public User GetUser(int userid)
    {
        session = GetCurrentSession();
        return session.Load<User>(userid);                         
    }

The webservice expects to serialize a user and NHibernate gives me a UserProxy (which is not a user exactly). How should I overcome this?


Solution

  • Don't return entities from the web method. Use a DTO.