Search code examples
c#silverlightwcf-ria-servicesdomainservices

SL: Why my DomainService returns my result queries as null?


I'm working right now with Silverlight and RIA Services.

In my project I have a DomainService and a AuthenticationService.

When I authenticate, I realized that if I debug my ObjectContext I can see all the records from my database.

But when I use my DomainService, I'm trying to get the objects from the default queries, for I.E. GetStudents but always the queries returns 0 elements.

But from it, I want to do a Insert, it works

            // Has finished
            var jsonObjects = JsonConvert.SerializeObject(Test, Formatting.Indented);

            var context = new DatabaseDomainContext();
            // it works!! add the object
            //Student newStudent = new Student();
            //newStudent.Id = "OPA-3DKCL2";
            //newStudent.FirstName = "Oscar";
            //newStudent.LastName = "Fimbres";

            //context.Students.Add(newStudent);
            //context.SubmitChanges();

            // all the time returns 0 elements
            var students2 = context.Load(context.GetStudentsQuery()).Entities;

            // the same
            var students = context.GetStudentsQuery();
            AnsweredTest answerTest = new AnsweredTest();
            answerTest.JsonTest = jsonObjects;
            answerTest.Date = DateTime.Now;
            //answerTest.Student = context.Students.SingleOrDefault(x => x.Id == "OPA-3DKCLS");

enter image description here

If I'm missing an important data, please let me know.


Solution

  • Load operation is asyncronious, you need to subscribe to Completed event and get result there:

    var loadOperation = context.Load(context.GetStudentsQuery());
    operation.Completed += OnStudentsLoaded;
    
    private void OnStudentsLoaded(object sender, EventArgs e)
    {
        var operation = sender as LoadOperation<Student>;
        if (operation == null)
        {
            throw new ArgumentException("sender is not LoadOpearation<Student>");
        }
        IEnumerable<Student> students = operation.Entities;
    
        //.....
    }