Search code examples
c#visual-studioweb-servicesvisual-studio-2010wcf-data-services

Get error message when returning back an Object from Web Service to client


I have programmed a desktop application with Visual Studio 2010, Entity Framework 4, which contains several projects inside of it:

  1. ECartableDataService (this is a web based data service and is uploaded on IIS Server)
  2. ECartableEntities
  3. ECartableModel
  4. ECartableRepository
  5. ECartableUI

Everything works fine, until I have decided to upgrade it to Visual Studio 2017 and Entity Framework 6.

When it comes to this part :

   using (ECartableServiceClient client = new ECartableServiceClient ())
   {
        User t = client.JustForTest();       
   }

I get following error:

An error occurred while receiving the HTTP response to http://Server2012:11545/ECartableDataService.svc. 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.'

JustForTest function is just a function for test and getting where is the problem, it resides in the web data service:

public User JustForTest()
{            
        User u = new User();   // User is an entity created by Entity Framework
        // bool flag = false;

        using (UnitOfWork unitOfWork = new UnitOfWork())
        {
            u = unitOfWork.UserRepository.GetTest(1135);
        }
           
        return u;
}

The problem arises when the function returns a User object, for example, if I set it returns a bool value (like flag), it works fine, but it returns an object like User or other entities, it throws an error.

I traced the web service and I got the message from it:

enter image description here

According to Serialization of Entity Framework objects with One to Many Relationship

I did the following

  context.Configuration.ProxyCreationEnabled = false;

but it didn't work out.

It's something about serialization, this application worked fine on Visual Studio 2010, but how can I return an object from a web service to UI in Visual Studio 2017?


Solution

  • I added KnownType attribute for all the Navigation Property of the Entity(User in here), it fixed,Like this:

    [KnownType(typeof(Task))]
    [KnownType(typeof(Folder))]
    public partial class User : StateObject
    {
        
        public User()
        {
            this.TasksSent = new HashSet<Task>();  
            this.Folder= new HashSet<Folder>();            
        }
    
        public short Id { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public byte Privilege { get; set; }
        public bool IsActive { get; set; }
        public string ProjectExclude { get; set; }
    
       
        public virtual ICollection<Folder> Folder{ get; set; }
        public virtual ICollection<Task> TasksSent { get; set; }
        
    }