Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Please excuse me if this is a duplicate question - I have searched but have not found anything that explains my problem.

I created an ASP.NET website. I have 3 layers - Data Access, Business, Presentation. I used shared entities throughout using Entity Framework.

  • Presentation: UI
  • Business: WCF Service
  • Data Access: code to connect to DB (contains the EF context, connection string)
  • Entities: .edmx - EF Entities. These are shared throughout all 3 layers.

I have come upon some odd behavior in the WCF service which I cannot understand. My original code had a function in the DAL which queried the db for all customers, and returned a List. The service than further queried this list based on what the UI requested and returned a List to the Presentation Layer.

DAL:

public List<Customer> GetCustomers()
    {
        List<Customer> custList= new List<Customer>();
        try
        {
            using (NorthWindsEntities context = new NorthWindsEntities(connectionString))
            {
                custList= context.Customers
                                 .Include("Orders")
                                 .Include("OrderDetails")
                                 .ToList();
            }
            return custList;
        }
        catch (Exception ex)
        {
            //handle exception
            return custList;
        }
    } 

WCF:

 public List<Customer> GetCustomersByState(string state)
    {
        contextManager contextDAL = new contextManager();
        List<Customer> custList = contextDAL.GetCustomers()
                                            .Where(c => c.State == state)
                                            .ToList();
        return custList;
    }

When I debugged the code, eveything worked fine, but when the service tried to return the custList to the client, I got this error: An error occurred while receiving the HTTP response to localhost/WCF/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.

I changed my DAL to inclued the entire query and my WCF Service to this:

  public List<customer> GetCustomersByState(string state)
    {
        contextManager contextDAL = new contextManager();
        List<Customer> custList = contextDAL.GetCustomers(state)
        return custList;
    }

This worked fine. I also tried to put the entire query in the service (the service connected directly to the context and got the list) and that worked fine as well.

So basically, my question is, why can't the WCF service return a list of objetcs that were queried using Linq to Objects (the DAL returns a List of customer so all further querying is linq to objects). It works fine using Linq to Entities (with all the code in the service) and it works fine with no querying at all - just getting a list and returning it.

I'm sorry for the lengthy post, I tried to include all necessary details...Thanks!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

After much trial and error, I have found that it is not a querying issue, it is a size issue. Though the WCF service was filtering my data to only return a few records, I guess it retained some sort of reference to the rest of the data because it errored with too much data. If I do a partial filter on the DAL and then continue to filter on WCF layer, coming out the same amount of records as I originally tried to return, it returns the list without a problem.

I cannot explain why this happens, I don't know much about WCF, just explaining what I did to get around it in case anyone else has this problem.

share|improve this answer
1  
Your DAL method is including two other objects - when your original WCF call took the customer information out of that collection, it brought the data in the related objects as well. This has nothing to do with WCF, btw (outside of the size limitations, which can be adjusted) - the root cause was the LINQ query. –  Tim Jul 15 '13 at 17:33
    
That makes sense. So how can I fix my query to make it work the original way? I need the 'Include' tables passed along with the customer. –  ploni Jul 15 '13 at 17:56
1  
You'll have to increase the size limits on your bindings. If you post your config files (service and client) I can show you how to do it, if you don't know how already. –  Tim Jul 15 '13 at 19:02
    
I did it and I made some modifications to the code as well and it now works! Thanks, Tim, for the explanation of why this happens. –  ploni Jul 15 '13 at 19:24
    
Remember to mark the accepted answers to your questions, even if you answered it yourself! –  Dan Ling Jul 15 '13 at 20:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.