使用 WCF REST 服务入门套件检索 EF4 POCO
我正在使用 WCF REST 服务(GET 方法)来检索我的 EF4 POCO。这项服务似乎运作得很好。当我在浏览器中查询 uri 时,我得到了预期的结果。
在我的客户端应用程序中,我尝试使用 WCF REST Starter Kit 的 HTTPExtension 方法 - ReadAsDataContract() 将结果转换回我的 POCO。当 POCO 的导航属性是相关 POCO 的单个对象时,这可以正常工作。问题是当导航属性是相关 POCO 的集合时。 ReadAsDataContract() 方法抛出异常,并显示消息“未将对象引用设置为对象的实例。”
下面是我的 POCO。
[DataContract(Namespace = "", Name = "Trip")]
public class Trip
{
[DataMember(Order = 1)]
public virtual int TripID { get; set; }
[DataMember(Order = 2)]
public virtual int RegionID { get; set; }
[DataMember(Order = 3)]
public virtual System.DateTime BookingDate { get; set; }
[DataMember(Order = 4)]
public virtual Region Region { // removed for brevity
}
}
[DataContract(Namespace = "", Name = "Region")]
public class Region
{
[DataMember(Order = 1)]
public virtual int RegionID { get; set; }
[DataMember(Order = 2)]
public virtual string RegionCode { get; set; }
[DataMember(Order = 3)]
public virtual FixupCollection<Trip> Trips { // removed for brevity
}
}
[CollectionDataContract(Namespace = "", Name = "{0}s", ItemName = "{0}")]
[Serializable]
public class FixupCollection<T> : ObservableCollection<T>
{
protected override void ClearItems()
{
new List<T>(this).ForEach(t => Remove(t));
}
protected override void InsertItem(int index, T item)
{
if (!this.Contains(item))
{
base.InsertItem(index, item);
}
}
}
这就是我尝试检索 Region POCO 的方法。
static void GetRegion()
{
string uri = "http://localhost:8080/TripService/Regions?id=1";
HttpClient client = new HttpClient(uri);
using (HttpResponseMessage response = client.Get(uri))
{
Region region;
response.EnsureStatusIsSuccessful();
try
{
region = response.Content.ReadAsDataContract<Region>(); // this line throws exception because Region returns a collection of related trips
Console.WriteLine(region.RegionName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
将不胜感激任何指点。
I am using WCF REST service (GET method) to retrieve my EF4 POCOs. The service seem to work just fine. When I query the uri in my browser I get the results as expected.
In my client application I am trying to use WCF REST Starter Kit's HTTPExtension method - ReadAsDataContract() to convert the result back into my POCO. This works fine when the POCO's navigation property is a single object of related POCO. The problem is when the navigation property is a collection of related POCOs. The ReadAsDataContract() method throws an exception with message "Object reference not set to an instance of an object."
Below are my POCOs.
[DataContract(Namespace = "", Name = "Trip")]
public class Trip
{
[DataMember(Order = 1)]
public virtual int TripID { get; set; }
[DataMember(Order = 2)]
public virtual int RegionID { get; set; }
[DataMember(Order = 3)]
public virtual System.DateTime BookingDate { get; set; }
[DataMember(Order = 4)]
public virtual Region Region { // removed for brevity
}
}
[DataContract(Namespace = "", Name = "Region")]
public class Region
{
[DataMember(Order = 1)]
public virtual int RegionID { get; set; }
[DataMember(Order = 2)]
public virtual string RegionCode { get; set; }
[DataMember(Order = 3)]
public virtual FixupCollection<Trip> Trips { // removed for brevity
}
}
[CollectionDataContract(Namespace = "", Name = "{0}s", ItemName = "{0}")]
[Serializable]
public class FixupCollection<T> : ObservableCollection<T>
{
protected override void ClearItems()
{
new List<T>(this).ForEach(t => Remove(t));
}
protected override void InsertItem(int index, T item)
{
if (!this.Contains(item))
{
base.InsertItem(index, item);
}
}
}
And this is how I am trying retrieve a Region POCO.
static void GetRegion()
{
string uri = "http://localhost:8080/TripService/Regions?id=1";
HttpClient client = new HttpClient(uri);
using (HttpResponseMessage response = client.Get(uri))
{
Region region;
response.EnsureStatusIsSuccessful();
try
{
region = response.Content.ReadAsDataContract<Region>(); // this line throws exception because Region returns a collection of related trips
Console.WriteLine(region.RegionName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Would appreciate any pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一件需要检查的事情是代理是否生成&延迟加载会破坏查询和返回结果的操作。事实上,您的属性都被标记为虚拟,这将导致生成代理并启用延迟加载。当您在这两个功能正常工作的情况下进行序列化时,它会对序列化器造成严重破坏。
在这种情况下我所做的是在返回数据的操作中我将其关闭,例如,
(请注意,我是在没有智能感知的帮助下凭记忆打字的...)
public List GetSomeTrips
{
context.ContextOptions.LazyLoadingEnabled=false;
contxt.ContetOptions.ProxyGenerationEnabled=false;
返回 context.Trips.ToList();
}
another thing to check is if proxy generation & lazy loading are breaking your operation that is querying and returning results. The fact that your properites are all marked virtual would cause proxies to be generated and lazy loading to be enabled. When you searialize with those two features working, it wreaks havoc on the serializer.
What I do in this case is in the operation that returns the data I turn those off e.g.,
(note I am typing from memory without the aid of intellisense...)
public List GetSomeTrips
{
context.ContextOptions.LazyLoadingEnabled=false;
contxt.ContetOptions.ProxyGenerationEnabled=false;
return context.Trips.ToList();
}
因为我无法对这个问题发表评论。您是否尝试过将公共虚拟 FixupCollection Trips 设置
为具有标准 get 的属性?放;
我在数据契约和定义自定义 getter 和 setter 方面遇到了困难。
该逻辑应该位于实体/对象本身中,因为它将被覆盖。
这对我来说解决了很多问题。
As I can not comment to the question. Have you tried setting public virtual FixupCollection Trips
as a properity with the standard get; set;
Ive had difficulties with datacontracts and defining custom getter and setters.
That logic should be in the entity / object itself as it will be overridden.
That has been a fix for a ton of things for me.
这可能已经在您的 POCO 中,只是没有包含在内 - 它们有构造函数吗?您可以尝试
这可能会解决空引用错误。如果这是您的类,则与
FixupCollection
相同。This may already be in your POCOs just not included - do they have constructors? You could try
That may solve the null ref error. Same in
FixupCollection
if that is your class.