DCSIMG
Sending complex typed data from web service to SilverLight client - Part I - Ran Wahle's blog

Ran Wahle's blog

Sending complex typed data from web service to SilverLight client - Part I

Sending complex typed data from web service to SilverLight client - Part I

In this post I'll explain in how to send complex typed data between web service and SilverLight  client
code. In order to avoid security issues I'll use JavaScript as mediator between my server code and SilverLight
code.

 

First - Let's have a look at a solution contains web server side code with all layers (Presentation that will host
silverlight  control , BL, DAL, Structures)
and another SilverLight project

Solution screenshot

Now, leaving out all server-side lower layers and focusing on presentation layer
and SilverLight code, let's have a look at what we'll demonstrate:

1. Getting complex typed data from the server via JavaScript .
2. Sending some of the data to SilverLight control
3. Presenting data on Silverlight control (A simple ListBox for that matter).

Step 1 - Create a server web method to provide as with the data we need:

[WebMethod(EnableSession = true)]   
 public List<Employee> GetAllEmployees()    
{        
        var result = new List<Employee>();       
        //Adding some employees to the collection       
        result.Add(new Employee()
       {EmployeeNumber = 1,   FirstName = "Ran", LastName = "Wahle"});        
        result.Add(new Employee()    
       {EmployeeNumber = 2,  FirstName = "Anonymous", LastName = "Employee"        });                                           result.Add(new Employee() 
        {EmployeeNumber = 3 , FirstName = "Known", LastName = "Employee"});               
                
         return result;   
 } 



 

Step 2 - Create object similar to the one in the server site.
At this stage the word "Why" might come to mind followed by a question mark...
The reason for that is simple - we cannot make reference from SilverLight project to
a regular .Net one because SilverLight runs on compact framework and therefore can have referenced to SilverLight projects only.

However, You might of course use the service referenced object if you   connect to a service, here we don't do it to avoid security failures may caused by connection service
directly from the Silverlight object.

public class Employee
{
    public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
public int EmployeeNumber { get; set; }
}

On the second part I'll explain how to serialize the objects through JSON.

kick it on DotNetKicks.com

Technorati Tags: ,

Comments

Maor David-Pur said:

Will you publish the full code for download?

# July 30, 2008 5:37 AM

Ran Wahle said:

Yes. Thanks for the remark.

# July 30, 2008 6:01 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 8, 2008 8:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: