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

问题描述

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

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

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

关于这部分:

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

我收到以下错误

在收到对http:// Server2012:11545 / ECartableDataService.svc的HTTP响应时发生错误。这可能是由于服务端点绑定未使用HTTP协议。这也可能是由于服务器终止了HTTP请求上下文(可能是由于服务关闭了)。请参阅服务器日志以获取更多详细信息。'

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

函数返回一个User对象时,就会出现问题,例如,如果我将其设置为返回布尔值(如标志),它可以正常工作,但它返回的对象为User或其他实体,则会引发错误

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

enter image description here

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

我做了以下

  context.Configuration.ProxyCreationEnabled = false;

但没有成功。

与序列化有关,此应用程序在Visual Studio 2010上运行良好,但是如何将对象从Web服务返回到Visual Studio 2017中的UI?

解决方法

我为实体(此处为用户)的所有导航属性添加了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; }
    
}