Monday, January 24, 2011

Create Silverlight Application

Create Silverlight Application

Here I am going to share one of the Silverlight applications which I did for RND purpose and also what are the problems I have face during this application
Silverlight application is subset of WPF (window presentation foundation). It fulfils the traditional ASP.NET application.
To create Silverlight application following items we need to install
1) Visual studio 2010
2) Silverlight 4.0 development toolkit (http://silverlight.codeplex.com/)
3) Silverlight runtime
Once you install Silverlight 4.0 development kit go to visual studio open new project you will get Silverlight in the left hand side. Here there are some temples also available that are already partially completed one if we want we can continue from this template. Here I am going to explain from empty template
Visual studio -> New Project -> Silverlight -> Silverlight Application





When I select Silverlight application we will get below popup here its ask where we need to deploy this Silverlight application (WPF Control) here there are three option we can deploy to new web project , web site or MVC web project. Suppose if this solution contain any other web project this it will ask whether need to deploy to existing application or new application and also we can select Silverlight version and whether do we need RIA services. Next RND blogs I will write Silverlight application with RIA services.




Below it show the solution it contain client application (Silverlight) and server application (Hosting application)



Within the box First one is Silverlight application it will run in Silverlight not .NET frame work other one work on .NET frame work. We couldn’t add normal DLL to Silverlight application (RNDSilverlight) it accept only Silverlight supportable class library because this is not run in frame work run on Silverlight framework.


When we build the Silverlight application create .XAP file within ClientBin folder. This is the one contain all XAML code for run silverlight.


Adding Services to our application
Just right click on the RNDSilverlight (Client Application) Add Services Reference and add this service to our application
Add services reference to Silverlight class as using RNDSilverlight.WCFServices; after that we can create proxy class to connect that services


Service1Client service1Client = new Service1Client();
service1Client.GetDataCompleted +=new EventHandler<getdatacompletedeventargs>(RetriveData);
service1Client.GetDataAsync("Pirasanth");
public void RetriveData(object sender, GetDataCompletedEventArgs e)
{
if (e.Error== null)
{
var data = e.Result;
}
}

Note before assign the data we need to check whether e.Error contain any value if yes we need to put some custom error message otherwise it will prompt big message.

In this binding after 1 minute I got time out issue so I have increase receive and sent timeout in the services Web.config like

<basichttpbinding>
<binding name="Binding1" closetimeout="04:50:00" opentimeout="04:50:00" receivetimeout="04:20:00" sendtimeout="04:20:00" maxbuffersize="655360000" maxbufferpoolsize="524288000" maxreceivedmessagesize="655360000">
<readerquotas maxdepth="2000000000" maxstringcontentlength="2000000000" maxarraylength="2000000000" maxbytesperread="2000000000" maxnametablecharcount="2000000000">
</binding>
</basichttpbinding>
</bindings>


But still after 1 min I got the time out issue like “WCF TimeOut Issue in Silver Light Application” so increase the time out we need to bind it manually as below

BasicHttpBinding WCFServiceBind = new BasicHttpBinding
{
CloseTimeout = new TimeSpan(0, 30, 0),
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
ReceiveTimeout = new TimeSpan(4, 0, 0),
SendTimeout = new TimeSpan(4, 0, 0),
OpenTimeout = new TimeSpan(0, 30, 0)

}; // WCF TimeOut Issue in Silver Light Application


EndpointAddress WCFServiceEndPoint = new EndpointAddress("http://localhost.RNDServices.svc");
service1Client = new Service1Client(WCFServiceBind, WCFServiceEndPoint);
service1Client.GetDataCompleted += new EventHandler<>( RetriveData);
service1Client.GetDataAsync("Pirasanth");



After that I didn’t get timeout issue even after 4 min. and also I have increate data buffer size also.

Note: - we couldn’t use other binding in Silverlight. Only BasicHttpBinding and TCP binding. To fix the timeout issue both services and client side we need to increase as I mention above

Now i will focus on binding datagride for that I am creating Silverlight supportable class library in that library contain class Student we can bind the data which we got from the WCF services. Before that we need to add this System.ComponentModel.DataAnnotations dll to this library for the validation and we need to implement IDataErrorInfo interface for validation



public class Student : IDataErrorInfo
{
[Range(0, 100)]
public int Age
{
get;
set;
}

public String Name
{
get;
set;
}
public string Error
{
get { throw new NotImplementedException(); }
}

public string this[string columnName]
{
get
{
return Validation(columnName);

}
}

private string Validation(string columnName)
{
switch (columnName)
{

case "Age":
if (condition)
{ return "Tax should be with in 0, 100 this range"; }
else
{ goto default; }
default:
return null;
}
}
}


After that we need to drag this datagride from the toolbar and set the binding Binding="{Binding Path=Name, Mode=TwoWay}".


After that we need to write below code to bind the ObservableCollection
public void RetriveData(object sender, GetListOfStudentCompletedEventArgs e)
{
if (e.Error== null)
{
var obj = e.Result;
ObservableCollection<student_si> lst = new ObservableCollection<student_si>();
foreach (Student item in obj)
{
lst.Add(new Student_Si
{
Age=item.Age,
Name=item.Name
});
}

gv.ItemsSource = lst;
}
}



In this way we can bind the list to datagride.

Note:- if we use RIA services we don’t need to create one layer easily and quickly we can create using RIA enable Silverlight application. But problem it only we need to go with certain way. Not that much flexible.


Amma Bhagavan's Songs