类型是一个接口或抽象类,不能被实例化。 C#发送对象,包含抽象对象名单 [英] Type is an interface or abstract class and cannot be instantiated. c# send a object with contain a list of abstract object

查看:1007
本文介绍了类型是一个接口或抽象类,不能被实例化。 C#发送对象,包含抽象对象名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象超的发布的,它有两个派生类的拍卖的和的购买的。另外我有一个类的类别的有出版物清单。

I have an abstract superClass "Publication" that has two derived classes Auction and Purchase. Also I have a class category that has a list of Publications.

在我休息的服务器端我送类别的清单,在客户端我收到范畴这份名单,并尝试投的对象分类列表。问题是,当我尝试投我得到如下错误的对象:

In my Rest Server side I send the a list of Category, in the client side I receive this list of Category and try to cast the object as a list of Categories. The problem is when I try to cast the object I get the follow error :

无法创建类型FrontOffice.Models.Publication的一个实例。类型是一个接口或抽象类,不能被实例化。路径[0] .Publications [0]。价格

Could not create an instance of type FrontOffice.Models.Publication. Type is an interface or abstract class and cannot be instantiated. Path '[0].Publications[0].Price'

我将粘贴下面的这一点,我使用的代码。

I will paste below of this, the code that I am using.

 //Rest Server Controller
 public IEnumerable<Category> GetAllCategory() {
     return listCategory;
 }

//Category Class
public class Category {
    public string Name { get; set; }
    public List<Publication> Publications { get; set; }
}

//Publication Entity
[DataContract]
[KnownType(typeof(Auction))]
[KnownType(typeof(Buyout))]
public abstract class Publication {
    [DataMember]
    public int PublicationID { get; set; }
    [DataMember]
    public string Title { get; set; }

    public Publication() {}

    public Publication(int PublicationID, string Title, string Description) {
        this.PublicationID = PublicationID;
        this.Title = Title;
    }
}

和在客户端我做的:

List<Category> listCategory = new List<Category>();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:50687/");

client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("Application/json"));
HttpResponseMessage response = client.GetAsync("api/categories/GetAllcategory").Result;

if (response.IsSuccessStatusCode) {
    listCategory = response.Content.ReadAsAsync<List<Category>>().Result;
}



我得到在我尝试实例listCategory行的错误。

I am getting the error in the line that I try to instance listCategory.

推荐答案

你所得到的原因一个 AggregateException 是因为你调用一个异步方法( ReadAsAsync ):

The reason you are getting an AggregateException is because you are calling an async method (ReadAsAsync):

listCategory = response.Content.ReadAsAsync<List<Category>>().Result;

在异步代码中,你可以(在某种程度上)运行代码的多个平行线。这些代码行的任何一个都可以抛出异常,并且由于这种性质,抛出异常的异步方法会抛出 AggregateException 。此异常的目的是收集或聚合,所有可能被抛出由代码平行线异常。

In async code, you can (sort of) run multiple parallel lines of code. Any one of these lines of code can throw an exception, and due to this nature, async methods that throw exceptions will throw an AggregateException. The purpose of this exception is to collect, or aggregate, all of the exceptions that could have been thrown by "parallel" lines of code.

然而,随着@Matthew豪根指出,你AggregateExceptions的99%将只是包装一个例外。你要做的是什么

However as @Matthew Haugen points out, 99% of your AggregateExceptions will just wrap a single exception. What you have to do is


  1. 找到了被包裹里面的例外,

  2. 要么找出原因的的异常被抛出,或者在你的问题张贴,并要求更多的帮助。

  1. find the exception that is wrapped inside it, and
  2. either figure out why that exception is being thrown, or post it in your question and ask for more help.

底线是,这个问题可能是在你的代码许多事情之一,需要更多的信息来解决它。由于异常,只有当 Category.Publications 不为空,我不知道你用的EntityFramework与延迟加载发生什么呢?如果是这样,它可能是与类别和出版之间的外键的问题。但是,这只是一个在黑暗中刺伤。

Bottom line is that the problem could be one of many things in your code, and more information is needed to solve it. Since the exception happens only when Category.Publications is not null, I wonder are you using EntityFramework with lazy loading? If so, it could be an issue with the foreign key between Category and Publication. But that's just a stab in the dark.

这篇关于类型是一个接口或抽象类,不能被实例化。 C#发送对象,包含抽象对象名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆