Hosting and Debugging SilverLight in Windows Azure

Objective

This article will give a pictorial representation of how a SILVERLIGHT application can be hosted and debugged on local development fabric of Windows azure

Note: This article is taken from original article written by JIM and can be read Here

Now a SILVERLIGHT application can be hosted in a Web Role. And on local development fabric application can be debugged also.

Step 1

Add .XAP as MIME type to IIS. To open IIS, click on Start button. After that open Run prompt and type INETMGR.


After selecting MIME type from left top corner, select Open Feature from Action tab.


After opening Open Feature from the Action tab select Add


Then add the MIME type.

 

Step 2

Create Azure service.

Select File -> New -> Cloud Services ->Windows Azure Cloud Service.

After that select ASP.Net Web Role and click on arrow button.

 

Step 3

Add a SILVERLIGHT application. Right click on solution then select Add New Projects and select SILVERLIGHT application from SILVERLIGHT tab.

 

Host the SILVERLIGHT application in existing Web Role.

Make sure checkbox for Silverlight debugging is enabled is checked.


Make Silverlight .aspx page as start page. Select aspx page from WebRole1 project.

Step 5

Add some code in XAML. I am adding a button on Silverlight page and on click event of button , I will display a message in messagebox in debug mode.

MainPage.Xaml

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Net;

    5 using System.Windows;

    6 using System.Windows.Controls;

    7 using System.Windows.Documents;

    8 using System.Windows.Input;

    9 using System.Windows.Media;

   10 using System.Windows.Media.Animation;

   11 using System.Windows.Shapes;

   12 

   13 namespace SilverlightApplication1

   14 {

   15     public partial class MainPage : UserControl

   16     {

   17         public MainPage()

   18         {

   19             InitializeComponent();

   20         }

   21 

   22         private void myButton_Click(object sender, RoutedEventArgs e)

   23         {

   24 

   25             MessageBox.Show("I am Debugging Silverlight in Azure ");

   26 

   27         }

   28     }

   29 }

   30 

<UserControl x:Class=”SilverlightApplication1.MainPage”xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d=http://schemas.microsoft.com/expression/blend/2008

xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006

 mc:Ignorable=”d”
d:DesignHeight=”300″ d:DesignWidth=”400″>
<Grid x:Name=”LayoutRoot” Background=”White”>
<Button x:Name=”myButton” Height=”200″ Width=”300″ Background=”Navy” Content=”ClickMe” Click=”myButton_Click”
/>
</Grid>
</UserControl>

 MainPage.Xaml.cs

   Output


 Step 6

Debugging Silverlight from Azure. Just put a breakpoint in c# code and you would able to debug the Silverlight application hosted in Azure.


On running,


 So Silverlight application is in debugging mode.
Conclusion
In this article, I discussed how to host and debug a Silverlight application in Windows azure. Thanks for reading,

LINQ to Object Part #3: Grouping

Objective

In this article, I am going to show, how we could achieve grouping in LINQ to object

I have created two classes for my explanation purpose. Student class is having details of students and Hostel class is having details of hostel. Both classes are having a properties HostelNumber common. I will be using this property to perform join operations in later articles. Both classes are as below.

Student.cs

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace LINQtoOBJECT1

    7 {

    8      public  class Student

    9     {

   10 

   11          public int RollNumber { get; set; }

   12          public string Name { get; set; }

   13          public int Section { get; set; }

   14          public int HostelNumber { get; set; }

   15     }

   16 }

   17 

   Hostel.cs                    

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace LINQtoOBJECT1

    7 {

    8    public class Hostel

    9     {

   10 

   11        public int HostelNumber { get; set; }

   12        public int NumberofRooms { get; set; }

   13     }

   14 }

   15 


To create collection of students and hostels, I am creating two static functions.

Function to return collection of students.

    1    static List<Student> GetStudents()

    2         {

    3             List<Student> students = new List<Student>

    4             {

    5                 new Student() { RollNumber = 1,Name =“Alex “ , Section =  1 ,HostelNumber=1 },

    6                 new Student() { RollNumber = 2,Name =“Jonty “ , Section = 2 ,HostelNumber=2 },

    7                 new Student() { RollNumber = 3,Name =“Samba “ , Section = 3 ,HostelNumber=1 },

    8                 new Student() { RollNumber = 4,Name =“Donald “ , Section = 3 ,HostelNumber=2 },

    9                 new Student() { RollNumber = 5,Name =“Kristen “ , Section = 2 ,HostelNumber=1 },

   10                 new Student() { RollNumber = 6,Name =“Mark “ , Section = 1 ,HostelNumber=2},

   11                 new Student() { RollNumber = 7,Name =“Gibbs “ , Section = 1 ,HostelNumber=1 },

   12                 new Student() { RollNumber = 8,Name =“Peterson “ , Section = 2 ,HostelNumber=2 },

   13                 new Student() { RollNumber = 9,Name =“collingwood “ , Section = 3 ,HostelNumber=1 },

   14                 new Student() { RollNumber = 10,Name =“Brian “ , Section = 3 ,HostelNumber=2 }

   15 

   16             };

   17 

   18             return students;

   19 

   20         }

 Function to return collection of hostels

    1  static List<Hostel> GetHostel()

    2         {

    3             List<Hostel> hostels = new List<Hostel>

    4             {

    5 

    6                 new Hostel(){HostelNumber=1 ,NumberofRooms = 100},

    7                 new Hostel(){HostelNumber= 2 ,NumberofRooms = 200}

    8             };

    9             return hostels;

   10         }


Now have a look on both codes below, one is using LINQ and other is using LOOP to retrieve data from the list and print.
Note: I will be using above two classes and functions for my entire sample below.
Grouping in LINQ


 Syntax of Groupby

Var result = FROM selectVariable IN dataSourceorList GROUPBY selectVariable.variable INTO RANGEVARIABLE Select new { };

Grouping by a single property

    1  class Program

    2     {

    3         static void Main(string[] args)

    4         {

    5 

    6 

    7           List<Student> lstStudents = GetStudents();

    8           var  lstStudentName = from r in lstStudents

    9                                                    group r by r.HostelNumber into rngStudent

   10                                                    select new

   11                                                    {

   12                                                        HostelID = rngStudent.Key,

   13                                                        Students = rngStudent

   14                                                    };

   15 

   16           foreach (var name in lstStudentName)

   17           {

   18               Console.WriteLine(name.HostelID);

   19               foreach (Student s in name.Students)

   20               {

   21                   Console.WriteLine(s.RollNumber + s.Name);

   22               }

   23           }

   24              Console.ReadKey();

   25 

   26         }

   27 

 

 

 Explanation

  1. I am grouping on property hostel number.
  2. rngStudent is range variable in which ; I am putting the intermediate result.
  3. I am selecting the result in as anonymous type.
  4. While printing the result , first I am printing the key of group by and then retrieving the object in foreach.

        1 foreach (var name in lstStudentName)

        2           {

        3               Console.WriteLine(name.HostelID);

        4               foreach (Student s in name.Students)

        5               {

        6                   Console.WriteLine(s.RollNumber + s.Name);

        7               }

        8           }

    Output


 Accessing the Group object

    1 foreach (var name in lstStudentName)

    2           {

    3               Console.WriteLine(name.HostelID);

    4               foreach (Student s in name.Students)

    5               {

    6                   Console.WriteLine(s.RollNumber + s.Name);

    7               }

    8           }

If you see the above loop , first I am looping the key for the group by clause and then , I am looping through IEnumerable LINQ object. This object is containing the real result.

Conclusion

In this article, I have discussed GROUPING in LINQ to OBJECT. Thanks for reading.

LINQ to Object Part #2: Filtering and Sorting

Objective

In this article, I am going to show, how we could achieve filtering and sorting in LINQ to object

I have created two classes for my explanation purpose. Student class is having details of students and Hostel class is having details of hostel. Both classes are having a properties HostelNumber common. I will be using this property to perform join operations in later articles. Both classes are as below.

Student.cs

namespace LINQtoOBJECT1

{

public class Student

{
public int RollNumber { get; set; }
public string Name { get; set; }
public int Section { get; set; }
public int HostelNumber { get; set; }
}
}

 Hostel.cs

namespace LINQtoOBJECT1

{

public class Hostel
{
public int HostelNumber { get; set; }
public int NumberofRooms { get; set; }
}}

To create collection of students and hostels, I am creating two static functions.

 Function to return collection of students.

static List<Student> GetStudents(){

List<Student> students = new List<Student>{

new Student() { RollNumber = 1,Name =“Alex “ , Section = 1 ,HostelNumber=1 },

new Student() { RollNumber = 2,Name =“Jonty “ , Section = 2 ,HostelNumber=2 },
new Student() { RollNumber = 3,Name =“Samba “ , Section = 3 ,HostelNumber=1 },
new Student() { RollNumber = 4,Name =“Donald “ , Section = 3 ,HostelNumber=2 },
new Student() { RollNumber = 5,Name =“Kristen “ , Section = 2 ,HostelNumber=1 },
new Student() { RollNumber = 6,Name =“Mark “ , Section = 1 ,HostelNumber=2},
new Student() { RollNumber = 7,Name =“Gibbs “ , Section = 1 ,HostelNumber=1 },
new Student() { RollNumber = 8,Name =“Peterson “ , Section = 2 ,HostelNumber=2 },
new Student() { RollNumber = 9,Name =“collingwood “ , Section = 3 ,HostelNumber=1 },
new Student() { RollNumber = 10,Name =“Brian “ , Section = 3 ,HostelNumber=2 }
};
return students;
}

Function to return collection of hostels

static List<Hostel> GetHostel(){

List<Hostel> hostels = new List<Hostel>
{
new Hostel(){HostelNumber=1 ,NumberofRooms = 100},
new Hostel(){HostelNumber= 2 ,NumberofRooms = 200}
};
return hostels;
}

 Now have a look on both codes below, one is using LINQ and other is using LOOP to retrieve data from the list and print.
Note: I will be using above two classes and functions for my entire sample below.
Filtering in LINQ


 In below code, I am filtering result. I am only fetching detail of student with RollNumber 1.


List<Student> lstStudents = GetStudents();

Student student = (from r in lstStudents where r.RollNumber == 1 select r).First();

Console.WriteLine(student.Name + student.RollNumber + student.HostelNumber + student.Section);

 If you see closely in above LINQ query, I am using First clause. Because if I am returning the output in a Student type and default return type of LINQ is IEnumerable. If I modify the above LINQ as below

 Student student = (from r in lstStudents where r.RollNumber == 1 select r);

 At compile time we can expect below error,


When we run the above code

Output

Now if I want to have, named of all the students whose roll number is more than 3 and who resides in hostel number 2.

List<Student> lstStudents = GetStudents();

IEnumerable<string> lstStudentName = from r in lstStudents where r.RollNumber > 3 && r.HostelNumber==2
select r.Name ;

foreach (string name in lstStudentName)
Console.WriteLine(name);

In above LINQ query, I am applying both filtering and projection.
Output


 Intermediate values
If I want to have intermediate values in my LINQ query then LET clause come into action. LET clause helps to reduce redundancy in WHERE clause.

IEnumerable<string> lstStudentName = from r in lstStudents let condition = r.RollNumber where condition > 3 && r.HostelNumber== 2
select r.Name ;

Let clause can be having complex statements.
Sorting in LINQ
OrderBy clause is used for purpose of sorting in LINQ.
Sorting a single property
Just before WHERE clause, OrederBy clause is used to sort the query result. In code Name of the students will be displayed in ascending order.

List<Student> lstStudents = GetStudents();
IEnumerable<string> lstStudentName = from r in lstStudents let condition = r.RollNumber orderby r.Name where condition > 3 && r.HostelNumber== 2
select r.Name ;

foreach (string name in lstStudentName)

Console.WriteLine(name);

 Output


 Sorting multiple properties
By putting comma between properties multiple properties can be sorted. Sorting will be done left to right. For roll number property I am sorting in descending order. This can be achieved by using keyword DESCENDIG. By default sorting is ascending.


List<Student> lstStudents = GetStudents();

IEnumerable<Student> lstStudentName = from r in lstStudents let condition = r.RollNumber orderby r.Name,r.RollNumber descending
where condition > 3 && r.HostelNumber== 2
select r ;

foreach (Student name in lstStudentName)

Console.WriteLine(name.RollNumber + name.Name);


 Conclusion
In next part, I will be explaining Grouping. I hope this article was useful to you. Thanks for reading.

LINQ to Object Part #1: Projection

Objective

In this article, I am going to show, how we could achieve projection in LINQ to object. I will explain different types of projection and how to achieve them over a collection using LINQ.

I have created two classes for my explanation purpose. Student class is having details of students and Hostel class is having details of hostel. Both classes are having a properties HostelNumber common. I will be using this property to perform join operations in later articles. Both classes are as below.

Student.cs

namespace LINQtoOBJECT1{

public class Student{

 public int RollNumber { get; set; }
public string Name { get; set; }
public int Section { get; set; }
public int HostelNumber { get; set; }
}
}

Hostel.cs

namespace LINQtoOBJECT1{

public class Hostel
{
public int HostelNumber { get; set; }
public int NumberofRooms { get; set; }
}}

 To create collection of students and hostels, I am creating two static functions.
Function to return collection of students.

static List<Student> GetStudents(){

List<Student> students = new List<Student>{

new Student() { RollNumber = 1,Name =“Alex “ , Section = 1 ,HostelNumber=1 },

new Student() { RollNumber = 2,Name =“Jonty “ , Section = 2 ,HostelNumber=2 },
new student() { RollNumber = 3,Name =“Samba “ , Section = 3 ,HostelNumber=1 },
new Student() { RollNumber = 4,Name =“Donald “ , Section = 3 ,HostelNumber=2 },
new Student() { RollNumber = 5,Name =“Kristen “ , Section = 2 ,HostelNumber=1 },

new Student() { RollNumber = 6,Name =“Mark “ , Section = 1 ,HostelNumber=2},

new Student() { RollNumber = 7,Name =“Gibbs “ , Section = 1 ,HostelNumber=1 },

new Student() { RollNumber = 8,Name =“Peterson “ , Section = 2 ,HostelNumber=2 },
new Student() { RollNumber = 9,Name =“collingwood “ , Section = 3 ,HostelNumber=1 },

new Student() { RollNumber = 10,Name =“Brian “ , Section = 3 ,HostelNumber=2 }
};
return students;
}
 

Function to return collection of hostels

static List<Hostel> GetHostel(){

List<Hostel> hostels = new List<Hostel>{
new Hostel(){HostelNumber=1 ,NumberofRooms = 100},
new Hostel(){HostelNumber= 2 ,NumberofRooms = 200}
};
return hostels;
}

Now have a look on both codes below, one is using LINQ and other is using LOOP to retrieve data from the list and print.
Note: I will be using above two classes and functions for my entire sample below.
Comparison between LINQ and LOOP

Using LINQ

List<Student> lstStudents = GetStudents();

var students = from r in lstStudents where r.RollNumber >= 3select r;
foreach (Student student in students)
{
Console.WriteLine(student.Name);
}

 Using LOOP

List<Student> lstStudents = GetStudents();
for (int i = 0; i < lstStudents.Count; i++)
{
if (lstStudents[i].RollNumber >= 3)
{
Console.WriteLine(lstStudents[i].Name);
}}

 Output

If we see the above code we can say about LINQ

  1. It is simple
  2. It is single line of statement.
  3. It is having good readability.
  4. It is IMPERATIVE. It does mean, we need to tell code only what to do not how to do.

And by looking at LOOP, we can say

  1. It is not simple
  2. It is having many statements.
  3. Readability is complex
  4. It is DECLARATIVE, it does mean, we need to tell code what to do as well how to do.

Projection in LINQ


 Projecting a single field property

In this projection, we only select single public properties from the resulted object.

 

List<Student> lstStudents = GetStudents();


IEnumerable<string> students = from r in lstStudents select r.Name;


foreach (string s in students)


Console.WriteLine(s);

While selecting a single property intellisense will work. In above query, I am selecting Name property of Student object and returning it to ther IEnumerable of string.

Output

Projecting into anonymous types

When there is requirement to use only part of the resultant object, Anonymous type can come into action. In this we can project part of resultant object into an anonymous type and return to the type var.


List<Student> lstStudents = GetStudents();

 var student = from r in lstStudents select new { r.RollNumber, r.Name };
foreach (var a in student)
{
Console.WriteLine(a.RollNumber + “, “ + a.Name);
}
 

If we want we can give property name to anonymous type as below ,


List<Student> lstStudents = GetStudents();

var student = from r in lstStudents select
new {AnonRoolNumber= r.RollNumber, AnonName= r.Name };

foreach (var a in student)
{
Console.WriteLine(a.AnonRoolNumber + “, “ + a.AnonName );
}

 The problem with anonymous type projection is that, we cannot return collection of anonymous type from the LINQ query.
Output


 Projecting into custom types

Let, we suppose we need to retrieve result from LINQ query to a custom class. In this case projection into custom class comes into play. Suppose we want to fetch hostelnumber and rollnumber from student object into a new custom class called studenthostel.

class StudentHostel{

public int CustomRollNumber { get; set; }
public int CustomHostelNumber { get; set; }
}}

 Now we will project the result of LINQ query into this custom class.

 List<Student> lstStudents = GetStudents();


IEnumerable<StudentHostel> student = from r in lstStudents select
new StudentHostel { CustomHostelNumber = r.HostelNumber , CustomRollNumber = r.RollNumber };

foreach (StudentHostel a in student{
Console.WriteLine(a.CustomRollNumber + “, “ + a.CustomHostelNumber );
}

 In above query I am projecting the resulted object into a custom class and returning that in IEnumerable of custom class StudentHostel in our case.
Output


 Conclusion

In next part, I will be explaining sorting and filtering. I hope this article was useful to you. Thanks for reading.