开发者

Retrieve EF4 POCOs using WCF REST services starter kit

开发者 https://www.devze.com 2023-02-14 17:56 出处:网络
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.

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 = "开发者_Go百科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.


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


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.


This may already be in your POCOs just not included - do they have constructors? You could try

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

That may solve the null ref error. Same in FixupCollection if that is your class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号