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 :
在我的Rest服务器端,我发送了一个Category列表,在客户端,我收到了这个Category列表,并尝试将该对象转换为Categories列表。问题是,当我尝试转换对象时,我得到以下错误:
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'
无法创建FrontOffice.Models.Publication类型的实例。 Type是接口或抽象类,无法实例化。路径'[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的行中收到错误。
1 个解决方案
#1
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
. The purpose of this exception is to collect, or aggregate, all of the exceptions that could have been thrown by "parallel" lines of code.
在异步代码中,您可以(排序)运行多行并行代码。这些代码行中的任何一行都可以抛出异常,并且由于这种性质,抛出异常的异步方法将抛出AggregateException。此异常的目的是收集或聚合“并行”代码行可能引发的所有异常。
However as @Matthew Haugen points out, 99% of your AggregateExceptions will just wrap a single exception. What you have to do is
然而正如@Matthew Haugen指出的那样,99%的AggregateExceptions只包装一个例外。你要做的是
- find the exception that is wrapped inside it, and
- 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? If so, it could be an issue with the foreign key between Category and Publication. But that's just a stab in the dark.
底线是问题可能是代码中的许多问题之一,需要更多信息来解决它。由于只有当Category.Publications不为null时才会发生异常,我想知道你是否在使用延迟加载的EntityFramework?如果是这样,它可能是类别和出版物之间的外键问题。但那只是在黑暗中刺伤。
#1
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
. The purpose of this exception is to collect, or aggregate, all of the exceptions that could have been thrown by "parallel" lines of code.
在异步代码中,您可以(排序)运行多行并行代码。这些代码行中的任何一行都可以抛出异常,并且由于这种性质,抛出异常的异步方法将抛出AggregateException。此异常的目的是收集或聚合“并行”代码行可能引发的所有异常。
However as @Matthew Haugen points out, 99% of your AggregateExceptions will just wrap a single exception. What you have to do is
然而正如@Matthew Haugen指出的那样,99%的AggregateExceptions只包装一个例外。你要做的是
- find the exception that is wrapped inside it, and
- 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? If so, it could be an issue with the foreign key between Category and Publication. But that's just a stab in the dark.
底线是问题可能是代码中的许多问题之一,需要更多信息来解决它。由于只有当Category.Publications不为null时才会发生异常,我想知道你是否在使用延迟加载的EntityFramework?如果是这样,它可能是类别和出版物之间的外键问题。但那只是在黑暗中刺伤。