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

前端 未结 6 1693
隐瞒了意图╮
隐瞒了意图╮ 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:44

    Maybe you need to disable lazyloading and proxycreation, like this:

      db.LazyLoadingEnabled = false;
      db.ProxyCreationEnabled = false;
    

    Doing this your wcf service should work.

    0 讨论(0)
  • 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 :

    <system.diagnostics>
    <sources>
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Information, ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add initializeData="C:/temp/tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="traceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    </system.diagnostics>
    

    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

    0 讨论(0)
  • 2020-12-18 02:52

    I had this problem and in my case, the issue was the WCF service was returning a class that had a property with only a getter and no setter. I was trying to prevent the property from being modified by the receiver. To get around this, see this...

    WCF Services and Object Constructors

    0 讨论(0)
  • 2020-12-18 02:55

    I had the same error.

    In my case I have a table with an int column called OEM. In the model layer I have a class (DTO) with that column represented by an Enum. There was a row in the table which value in OEM colum was not valid. When I was trying to bring all data using LINQ, there was an error that wasn't captured by VisualStudio. That error raised when WCF tried to retrieve the message.

    0 讨论(0)
  • 2020-12-18 03:04

    None of the methods worked for me. What I did was to debug the service while running from the WCF Test Client and I was able to find my issue. It was to do with the service library missing the connection string in the app.config.

    Just posting if anyone has tried all the other methods and still unable to find the issue.

    0 讨论(0)
  • 2020-12-18 03:08

    Found the root cause after a lot of swearing and thinking of how nice the weather is outside. I remove the virtual keyword from the UserDetails object that is inside the User object.

    Now it works!

    As far as why this caused an issue, my assumptions are serialization or DbContext issues, but I will have to look more into it, not sure.

    I'm going outside now.

    So for reference, if you wound up here and have no idea what is going on, among all the other things you should look at (size, timeout, etc):

    Check to see if your object has virtual keyword on it.
    
    0 讨论(0)
提交回复
热议问题