WCF - An error occurred while receiving the HTTP response to http://xxxxx/Service/

前端 未结 6 1735
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 02:27

I am making this call in my WCF service:

public User GetStudentRecord(string userName)
    {
        try
        {
            return new DashboardFacade().G         


        
6条回答
  •  清酒与你
    2020-12-18 02:51

    I had the same Exception but none of the answers above gave me a solution. I figured out the error by defining a trace in web.config :

    
    
      
        
          
            
          
          
            
          
        
      
    
    
    

    I ran the website locally, fired a few failing calls and the opened the tracelog selecting the failed entry, inside that entry I saw this exception :

    The entity or complex type 'Person' cannot be constructed in a LINQ to Entities query.

    Constructing the person outside of the query fixed the issue so instead of :

     var persons = (from p in db.Persons
                      select new Person
                      {
                          Id = p.Id,
                          Name = p.Name,
                          LastName = p.LastName,
                          Email = p.Email
                      }
                  ).AsEnumerable();
    
      return persons;
    

    I had to do :

    var persons = (from p in db.Persons
                      select new 
                      {
                          Id = p.Id,
                          Name = p.Name,
                          LastName = p.LastName,
                          Email = p.Email
                      }
                  ).AsEnumerable()
                    //construct the complex type outside of DB  (to prevent the exception that model cannot be constructed in linq to entities)
                    .Select(item =>
    
                        new Person
                        {
                            Id = item.Id,
                            Name = item.Name,
                            LastName = item.LastName,
                            Email = item.Email
                        }).ToList(); 
    
                return persons;
    

    I could have saved alot of time if the exception message was more comprehensive, judging by all the different causes outlined in this topic I guess people will agree with me.

    Anyway, this is how I figured out the issue, hopefully this will help someone else to

提交回复
热议问题