1

I'm writing a WCF service with EF. My problem occurs when trying to return the children of customer entity. Below the sample code:

[DataContract]
public class Customer
{
        [Key]
        [DataMember]
        public int CustomerID { get; set; }

        [DataMember]
        public string FirstName { get; set; }

// Customer has a collection of BankAccounts
        [DataMember]
        public virtual ICollection<BankAccount> BankAccounts { get; set; }
}

[DataContract(IsReference = true)]
    public class BankAccount
    {
        [Key]
        [DataMember]
        public int BankAccountID { get; set; }

        [DataMember]
        public int Number { get; set; }

        // virtual property to access Customer
        //[ForeignKey("CustomerID")]
        [Required(ErrorMessage = "Please select Customer!")]
        [DataMember]
        public int CustomerID { get; set; }

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

The error i get:

An error occurred while receiving the HTTP response to http://localhost:8732/Design_Time_Addresses/MyServiceLibrary/MyService/. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

The service function that i call:

public Customer GetCustomer(int customerId)
        {
            var customer = from c in dc.Customers
                           where c.CustomerID == customerId
                           select c;
            if (customer != null)
                return customer.FirstOrDefault();
            else
                throw new Exception("Invalid ID!");

        }

I tried to debug it, and this function returns the Customer and it's children BankAccounts, i also disabled lazy loading. I found out that if i comment out this line

public virtual ICollection<BankAccount> BankAccounts { get; set; } 

form customer class, everything works except i can't get the BankAccount, it only returns the Customer. I'm new to WCF so please help me out. Thanx.

So i found how to fix the problem. Just had to mark Customer reference from BankAccount as IgnoreDataMember

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

and diable ProxyCreation in MyDbContext constructor.

this.Configuration.ProxyCreationEnabled = false;
tshepang
  • 12,111
  • 21
  • 91
  • 136
laszl0
  • 51
  • 6
  • A tip: Instead of throwing Exception with if-statement, just use "return customer.Single();", which will throw it for you. Single should be used when you expect exactly one match. – cederlof May 08 '12 at 10:27
  • It is because the navigation properties are virtual. See [this answer](http://stackoverflow.com/a/15822764/704144). – Şafak Gür Mar 13 '14 at 07:46

1 Answers1

0

Your function requires lazy loading enabled to return both customer and his accounts because the function doesn't use eager loading. If you want to disable lazy loading you must use:

dc.Customers.Include("BankAccounts")

Anyway your main problem are cyclic references (customer has reference to accounts and account has backward reference to the customer) which breaks default WCF serialization. You must inform serializer about their existence.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thank you for the response, but my problem still persists. – laszl0 May 08 '12 at 10:53
  • Did you follow the link I provided in the answer where? – Ladislav Mrnka May 08 '12 at 14:28
  • Yes i did, and there i saw the picture where you show that the EF4 entity can't be serialized, so i checked mine and mine looked the same, so i just disabled ProxyCreation. Still i didn't fully understand what ProxyCreation does, i'm still new to all of this. Thank you. – laszl0 May 09 '12 at 11:00