none
Issue with WCF and Entity Framework and child nested classes RRS feed

  • Question

  • I have 2 classes generated using entity framework. And I have 1 WCF method that retrieves data and i want to be able to include the child nested class data when calling the method. When i call the method from my client code I get a "{"The underlying connection was closed: An unexpected error occurred on a receive."}. I searched online and this is normally caused because lazy loading is enabled and it doesnt play nice with WCF. So i modified my wcf service setting the dbcontext proxycreationenabled property to false. That solves the issue of the error message, but the nested child object is empty. Is there anyway to use the built in navigation properites of EF with WCF? In the example below I want to get all vendorSystems for each vendor with one wcf call.

    Here are my classes (simplified)

     public partial class Vendor
        {
      
            
            public Vendor()
            {
                this.VendorSystems = new HashSet<VendorSystem>();
            }
    
            [DataMember]
            public int VendorId { get; set; }
    
       [DataMember]
            public virtual ICollection<VendorSystem> VendorSystems { get; set; }
    
    }
    and VendorSystem
    public partial class VendorSystem
        {
    
            public VendorSystem()
            {
                this.VendorSystemTemplates = new HashSet<VendorSystemTemplate>();
            }
    
            [DataMember]
            public int VendorSystemID { get; set; }
    
            [DataMember]
            public int VendorId { get; set; }
    
            [DataMember]
            public string Name { get; set; }
    
    }
    In my WCF method:
       public Vendor GetVendorInfo(string vendorName)
            {
                var db = new EmailVendorEntities();
    
    
                //db.Configuration.ProxyCreationEnabled = false; //setting this fixes error but nested object is empty
    
                return db.Vendors.FirstOrDefault(a => a.Name == vendorName);
    
    }

    Monday, September 21, 2015 11:28 PM

Answers

All replies

  • Hi,

    We can use .include method to archieve eager loading. Like:

    db.Vendors
    .where(a => a.Name == vendorName)
    .Include(a => a.VendorSystems)
    .FirstOrDefault();
    
    

    For more information, please refer to the document:

    http://blog.rsuter.com/using-entity-framework-code-first-proxies-with-wcf-web-services/

    Tuesday, September 22, 2015 6:53 AM
  • That solves the issue of the error message, but the nested child object is empty. Is there anyway to use the built in navigation properites of EF with WCF?

    Most would leave the EF entities behind the WCF service, and they would send DTO(s) Data Transfer Objects as datacontracts between the WCF client and service.

    Tuesday, September 22, 2015 2:07 PM
  • Can you explain DTO? Is it basically an identical set of classes I have already created but are used for transferring data?
    Tuesday, September 22, 2015 3:21 PM
  • This populates the class correctly, but when my calling application consumes the WCF method i get the  "{"The underlying connection was closed: An unexpected error occurred on a receive."}.


    Must be something with serialization. I set the following as others have recommended but still same error. Any other ideas?

    Configuration.LazyLoadingEnabled = false;
    Configuration.ProxyCreationEnabled = false;

    Tuesday, September 22, 2015 4:21 PM
  • Can you explain DTO? Is it basically an identical set of classes I have already created but are used for transferring data?

    The link explains a DTO. And yes I use DTO(s) and send them between the tiers in n-tier or between the WCF client and service using EF or not using EF.  

    https://en.wikipedia.org/wiki/Data_transfer_object

    That's why there a VS plug in tool that creates the DTO(s) that are datacontracts to WCF.

    https://entitiestodtos.codeplex.com/

    The solution never went beyond VS2010. But I was rolling my own DTO(s) before the VS plugin and after it was not made to work after VS2010 and using EF.

    Tuesday, September 22, 2015 4:41 PM
  • This populates the class correctly, but when my calling application consumes the WCF method i get the  "{"The underlying connection was closed: An unexpected error occurred on a receive."}.

    You have to get the stacktrace and the inner exception message by using a try/catch with ex.Tostring(). The inner exception message if any will expose the true error.

    https://msdn.microsoft.com/en-us/library/system.exception.tostring%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Tuesday, September 22, 2015 4:44 PM