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

29 ביולי 2008

2 comments

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: ,

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 comments

  1. Maor David30 ביולי 2008 ב 5:37

    Will you publish the full code for download?

    Reply
  2. Ran Wahle30 ביולי 2008 ב 6:01

    Yes. Thanks for the remark.

    Reply