从 Web 服务向客户端返回对象时获取错误消息

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

我用 Visual Studio 2010,Entity Framework 4 编写了一个桌面应用程序,其中包含几个项目:

  1. ECartableDataService(这是一个基于网络的数据服务,上传到 IIS 服务器)
  2. ECartableEntities
  3. ECartableModel
  4. ECartableRepository
  5. ECartableUI

一切正常,直到我决定将其升级到 Visual Studio 2017 和 Entity Framework 6.

说到这部分:

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

我收到以下错误:

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函数只是一个测试函数,问题出在哪里,它驻留在web数据服务中:

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;
}

当函数 return 是一个 User 对象时,问题就出现了,例如,如果我将它设置为 return 一个 bool 值(如标志),它工作正常,但它return 像 User 或其他实体这样的对象,它会引发错误。

我跟踪了 Web 服务并从中得到了消息:

根据Serialization of Entity Framework objects with One to Many Relationship

我做了以下事情

  context.Configuration.ProxyCreationEnabled = false;

但没有成功。

这与序列化有关,此应用程序在 Visual Studio 2010 年运行良好,但我如何 return 一个对象从 Web 服务到 UI 在 Visual Studio 2017 ?

我为实体(此处为用户)的所有导航 属性 添加了 KnownType 属性,它已修复,像这样:

[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; }
    
}