简体   繁体   English

类型是接口或抽象类,无法实例化。 c#发送一个包含抽象对象列表的对象

[英]Type is an interface or abstract class and cannot be instantiated. c# send a object with contain a list of abstract object

I have an abstract superClass " Publication " that has two derived classes Auction and Purchase . 我有一个抽象的超类“ 出版物 ”,它具有两个派生类AuctionPurchase 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. 在我的Rest Server端中,我发送了一个Category列表,在客户端中,我收到了这个Category列表,并尝试将对象转换为Categories列表。 The problem is when I try to cast the object I get the follow error : 问题是,当我尝试投射对象时,出现以下错误:

Could not create an instance of type FrontOffice.Models.Publication. 无法创建类型FrontOffice.Models.Publication的实例。 Type is an interface or abstract class and cannot be instantiated. 类型是接口或抽象类,无法实例化。 Path '[0].Publications[0].Price' 路径“ [0]。出版物[0]。价格”

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

And in the client side I do : 在客户端,我这样做:

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

I am getting the error in the line that I try to instance listCategory. 我在尝试实例化listCategory的行中遇到错误。

The reason you are getting an AggregateException is because you are calling an async method ( ReadAsAsync ): 得到AggregateException的原因是因为您正在调用异步方法( ReadAsAsync ):

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

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 . 这些代码行中的任何一行都可以引发异常,并且由于这种性质,引发异常的异步方法将引发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. 此异常的目的是收集或汇总“并行”代码行可能抛出的所有异常。

However as @Matthew Haugen points out, 99% of your AggregateExceptions will just wrap a single exception. 但是,正如@Matthew Haugen指出的那样,您99%的AggregateExceptions只会包装一个异常。 What you have to do is 你要做的是

  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. 弄清楚为什么会引发异常,或者将其发布在您的问题中并寻求更多帮助。

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? 由于仅在Category.Publications不为null时发生异常,所以我想知道您是否在使用EntityFramework进行延迟加载? If so, it could be an issue with the foreign key between Category and Publication. 如果是这样,类别和发布之间的外键可能是一个问题。 But that's just a stab in the dark. 但这只是黑暗中的一击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReadAsAsync - 错误:“Type是一个接口或抽象类,无法实例化。” - ReadAsAsync - Error : “Type is an interface or abstract class and cannot be instantiated.” C#JSON反序列化:Type是接口或抽象类,无法实例化 - C# JSON Deserialization : Type is an interface or abstract class and cannot be instantiated 类型是接口或抽象类,不能被实例化 - Type is an interface or abstract class and cannot be instantiated 无法创建类型的实例。 类型是接口或抽象类,不能被实例化 - Could not create an instance of type . Type is an interface or abstract class and cannot be instantiated 为什么不能实例化抽象类和接口? - Why abstract class and interface cannot be instantiated? 具有动态类型的C#接口/抽象类 - C# Interface/Abstract Class with Dynamic Type C#抽象类具有另一个抽象类对象 - C# Abstract class has another abstract class object 如何获取分配给 C# 中抽象类的泛型列表中的对象类型? - How can I get the type an object in a generic list assigned to an abstract class in C#? C#接口和抽象类 - C# interface and abstract class Json.NET - 反序列化接口属性抛出错误“类型是接口或抽象 class,无法实例化” - Json.NET - Deserialising interface property throws error "type is an interface or abstract class and cannot be instantiated"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM