【问题标题】:Get error message when returning back an Object from Web Service to client将对象从 Web 服务返回到客户端时收到错误消息
【发布时间】:2021-01-26 10:05:42
【问题描述】:

我使用 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();       
   }

我收到以下错误:

接收对 http://Server2012:11545/ECartableDataService.svc 的 HTTP 响应时出错。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。'

JustForTest函数只是一个用于测试和找出问题所在的函数,它驻留在网络数据服务中:

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 服务并从中得到了消息:

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

我做了以下

  context.Configuration.ProxyCreationEnabled = false;

但没有成功。

这是关于序列化的事情,这个应用程序在 Visual Studio 2010 上运行良好,但我如何将 Web 服务中的对象返回到 Visual Studio 2017 中的 UI?

【问题讨论】:

    标签: c# visual-studio web-services visual-studio-2010 wcf-data-services


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      相关资源
      最近更新 更多