# Tuesday, 16 December 2008

This took me quite a while to figure out, so I thought I’d post it. :) 

I’ve got a Silverlight application that calls a web service, that gets some data from SQL Server via LINQ.  The LINQ statement returns an IEnumerable<QuestionCategory>, and each QuestionCategory has a property which is IEnumerable<Question>:

 

    public class QuestionCategory

    {

        public string CategoryName { get; set; }

        public int CategoryId { get; set; }

        public int Order { get; set; }

        public IEnumerable<Question> Questions { get; set; }

    }

 

    public class Question

    {

        public string Text { get; set; }

        public ScaledAnswer? Answer { get; set; }

    }

            using (EvalDataContext context = new EvalDataContext())

            {

                var x = from category in context.EvalQuestionCategories

                        select new QuestionCategory()

                        {

                            CategoryId = category.CategoryID,

                            CategoryName = category.CategoryText,

                            Order = (int)category.CategoryOrder,

                            Questions = (from q in category.EvalQuestions select new Question() { Text = q.QuestionText }).ToList()

                        };

                return x.ToList();

            }

This all worked just fine until it got across the wire to the Silverlight client, where I was consistently getting “The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.”  Helpful?  Sort of.  The first step was to track down what was actually getting serialized on the server side, and why that should be a problem.  Several debugger sessions later, turns out the “Questions” property of the QuestionCategory was really an ObjectMaterializer (an internal LINQ class) and not really a collection of Question objects, despite (and this is the part that really got me) the fact that I was explicitly calling .ToList() on the LINQ expression.  (FYI: .ToArray() produced the same results)

Given the above, it makes sense that this counts as “not a meaningful reply”, but what’s the solution?  ToList or ToArray should have caused the LINQ statement to be realized, I would think, but no.  In the end, it was pretty straightforward, although I’m not sure I agree that it should be necessary. 

    public class QuestionCategory

    {

        public string CategoryName { get; set; }

        public int CategoryId { get; set; }

        public int Order { get; set; }

        public List<Question> Questions { get; set; }

    }

By changing the Questions property from IEnumerable<Question> to List<Question> it was realized in time, and everything was serialized/deserialized properly. 

It took an embarrassingly long time to figure this out, so hopefully this will be googled by some other poor soul facing the same problem. :-)

Tuesday, 16 December 2008 11:19:03 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [3]  | 
# Friday, 12 December 2008

I finished up on a customer project this week, and now it’s full time on Education.  We’ve got a ton of new stuff coming with the new year, including some exciting events (details hopefully next week) and some new classes.  We’ll be teaching WPF, Practical .NET Debugging, and some new Silverlight classes in Q1, plus whatever your organization needs delivered at your facility or ours.  If your organization needs any custom training, or you have questions about course offerings, please drop me a line at education@sftsrc.com.

Friday, 12 December 2008 12:55:14 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |