Tuesday, November 15, 2011

Consume / Call a Web Service in Silverlight application

Hi All,
       For all guys looking for how to Call  or Consume a Web Service in Silverlight  then this article is for you .
First of I would like to tell you that I am using Silverlight 4 and Visual Studio 2010.


So First of all create a Silverlight Application and name it anything say  "CBPSilverlight".




Ensure the Check box (Host the Silverlight application in a new Web Site) remains checked Under the "New Web Project Type" dropdown select "ASP.NET Web Site .
Note mine is Silverlight 4 version .Click OK.



Now you will see the Solution explorer as containing 2 Projects now:




Now we add a Web Service that will have web methods to be called inside the Silverlight application.


Right Click on CBPSilverlight WebSite and add new Item as a web Service. Name it as say " SLwebservice"



You will notice a SLwebservice.cs is open now under App_code folder.
This web service is populated with default "Hello World" Web Method.

Add one more Web Method say  GetTodaysDate() which will return todays date and time.
So now your method will look like this ( See both Web Method are higlighted )


Now once your Web Methods are completed , it is now time to consume in the Silverlght application . 
Now right click under the CBPSilverlight application's References' node ( not the Web Site)  and click " Add Service Reference..."



If you are not able to find the Services just click on Discover to find out the SLwebservice.
Give a name to the reference under Namespace column say " SLWebReference" and click OK.







Now in teh CBSilverlight application open MainPage.xaml (designer) and the following StackPanel inside the Grid Tag.




After that go to MainPage.xaml.cs and add a namespace reference for web service . Note web service reference name is qualified by prefixing with Application  name . see below:








Now the final step is to call the Web service (SLwebservice) here.
but remember : 

  1. All Web service calls in Silverlight are asynchronous.
  2. The web service proxy contains two members for each operation in the service: an asynchronous method and a completed event. For example, consider the “HelloWorld” service operation. We first add anEventHandler to HelloWorldCompleted within the scope of the MainPage() constructor. This is the event that will be invoked when the service returns the data we requested. After the event is set up, we are ready to make the call to the service by calling HelloWorldAsync method. 







Now add the following code just under InitializeComponent() in MainPage() constructor.:




            Sclient.HelloWorldCompleted += new EventHandler<HelloWorldCompletedEventArgs>(Sclient_HelloWorldCompleted);
            Sclient.HelloWorldAsync();

            Sclient.GetTodaysDateCompleted += new EventHandler<GetTodaysDateCompletedEventArgs>(Sclient_GetTodaysDateCompleted);
            Sclient.GetTodaysDateAsync();


Now add below two Events that are  mentioned above : 


        void Sclient_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)
        {
            textBlock1.Text = e.Result.ToString();
        }

        void Sclient_GetTodaysDateCompleted(object sender, GetTodaysDateCompletedEventArgs e)
        {
            textBlock2.Text = e.Result.ToString();
        }







So finally your code will look like this : 






Save your Project and Hit F5 or Ctrl+f5 and see the results on the asp.net web site Page.


Below  is the running page : 



Hope you find this post Helpful..
Happy Coding ...
Thanks,
Nitin Sharma