Yuck
Yuck

Reputation: 50855

WCF and Entity Framework with Telerik GridModel

This is cross-posted on Telerik's support forums.

I have an Entity Framework (Code First, 4.1) context that is used in a WCF service layer. If I have the service set up this way:

public IEnumerable<Dto.PlanMember> GetPlanMembers() {
    var planMembers = from member in this.DataSource.PlanMemberDbSet.Take(10)
                      select new Dto.PlanMember {
                          Id = member.Id,
                          FirstName = member.FirstName,
                          LastName = member.LastName
                      };

    return planMembers;
}

The service works fine. If I set it up like this, following the examples for grid binding via web services...

public GridModel GetPlanMembers(GridState state) {
    var planMembers = from member in this.DataSource.PlanMemberDbSet.Take(10)
                      select new Dto.PlanMember {
                          Id = member.Id,
                          FirstName = member.FirstName,
                          LastName = member.LastName
                      };

    return planMembers.ToGridModel(state);
}

It consistently get the exception:

The underlying connection was closed: The connection was closed unexpectedly.

I'm certain this is a serialization issue. For whatever reason the ToGridModel extension method isn't working properly with EFCF 4.1. What can be done to fix this?

This doesn't work either, so it must be related to serializing the GridModel type and not the extension method itself that's the problem:

public GridModel GetPlanMembers(GridState state) {
    var planMembers = from member in this.DataSource.PlanMemberDbSet.Take(10)
                      select new Dto.PlanMember {
                          Id = member.Id,
                          FirstName = member.FirstName,
                          LastName = member.LastName
                      };

    var gridModel = new GridModel {
        Data = planMembers,
        Total = planMembers.Count()
    };

    return gridModel;
}

Upvotes: 0

Views: 590

Answers (1)

cadrell0
cadrell0

Reputation: 17327

The issue is that LINQ queries have delayed execution. So you are returning an IQueryable. The data is not actually retrieved until WCF tries to serialize it. The problem is that your connection is already closed at that point. Throw a .ToList() on there and you should be good.

Upvotes: 2

Related Questions