Get POCO EF4 with the WCF REST Services Starter Kit

I am using the WCF REST service (GET method) to retrieve the POCO EF4. The service seems to be working fine. When I request uri in my browser, I get the results as expected.

In my client application, I am trying to use the HTTPExtension method for the WCF REST Starter Kit - ReadAsDataContract () to convert the result back to my POCO. This works great when the POCO navigation property is the only related POCO object. The problem is that the navigation property is a collection of related POCOs. The ReadAsDataContract () method throws an exception with the message "Object reference not installed in the object instance."

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 try to get the POCO Region .

 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 thank any pointers.

+4
source share
3 answers

another thing to check is that proxy generation and lazy loading disrupt the job that requests and returns results. The fact that all your rights are marked virtual will lead to the creation of proxies and lazy loading. When you hum with these two working functions, it is detrimental to the serializer.

In this case, I do an operation that returns data that I disabled, for example (note that I print from memory without the help of intellisense ...)

open list GetSomeTrips {context.ContextOptions.LazyLoadingEnabled = false; contxt.ContetOptions.ProxyGenerationEnabled = false;
return context.Trips.ToList (); }

+2
source

How I can not comment on the question. Have you tried setting up the public virtual FixupCollection Trips

as a property with standard get; install;

I had difficulties with datacontracts and defining custom getters and seters. This logic must be in the entity / object itself, as it will be redefined.

That was a problem for me.

+1
source

It may already be in your POCOs that are simply not included - do they have constructors? You can try

 public Region(){ this.Trips = new FixupCollection<Trip>(); } 

This may solve the null ref error. Same thing in FixupCollection if this is your class.

0
source

Source: https://habr.com/ru/post/1341895/


All Articles