1

I'm trying to use nHibernate, Spring and WCF together. I've got an Order object, and that contains a Customer object.

I can call a WCF method findOrder on my service, and providing the Order's Customer field does not have a DataMember annotation, the Web Service returns the Order I wanted. It does not contain the Customer details though, as expected.

But when I try to include the Customer as well, the WebService fails, and looking in the WCF trace logs, I can see this error:

System.Runtime.Serialization.SerializationException: Type 'DecoratorAopProxy_95d4cb390f7a48b28eb6d7404306a23d' with data contract name 'DecoratorAopProxy_95d4cb390f7a48b28eb6d7404306a23d:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer

Pretty sure this is because the Customer contains extra nHibernate details, but I don't understand why WCF would be happy to send the Order, but not the Customer.

Can anyone help me understand?

Order object

[DataContract]
[KnownType(typeof(Customer))]

public class Order
{
  // Standard properties
  [DataMember]
  public virtual int Id { get; set; }

  public virtual Enums.OrderStatus Status { get; set; }        

  [DataMember]
  [StringLength(20, ErrorMessage = "Order name must not be more than 20 characters long")]
  public virtual string Name { get; set; }

  [DataMember]
  public virtual Customer Customer { get; set; }

  [DataContract]
  ...
}

Customer object

public class Customer
{
    public virtual int CustomerId { get; set; }

    [DataMember]
    private string name = "";
    ...
}
Andreas
  • 4,581
  • 29
  • 39
Richard
  • 1,675
  • 2
  • 23
  • 50

1 Answers1

3

You should use a data transfer objects (DTO) to get your data over the wire. This is good practice anyway as you do not want to let your domain model leak into (and out of) the boundaries of your application.

Think about things like every change in your domain model results in a change of your data contract, resulting in a new wsdl, resulting in a change on the client. In addition you are telling the consumer of your service too many insights of your aplication.

Despite all this architectural bla bla. NHibernate uses proxies to enable lazy loading, those proxies are of another type than you serializer expects. You can disable lazy loading for your domain to get the application working. This is imho a bad idea.

<class name="Customer" table="tzCustomer" lazy="false" >
Andreas
  • 4,581
  • 29
  • 39
  • Thanks Andreas, I had a feeling that using DTOs would be the way forward. This is the first Application we have had to split into a web service layer that uses Spring/Hibernate so I hadn't run into the issue before. Your answer makes perfect sense though. – Richard Apr 27 '12 at 13:52