Consuming WCF Service in Silverlight4 Projects

Silverlight applications cannot access server-side classes or variables without mediator like Web Service or WCF Service or WCF RIA. Now I will walk through how we can call WCF Service in Silverlight4 application.
We need to install silverlight4 SDK to work on Silverlight projects. Please follow this blog post for more details on Silverlight Tool KIT installation.
To create a Silverlight application, Go to File- >New-> Silverlight-> Silverlight Application
Name it as demo; Silverlight Application requires a web application project or website to host it.
You can view Client-Bin Folder here in the web application that will be empty initially.
But when we compile the application, Silverlight project gets compiled into an assembly, and this assembly gets wrapped into the XAP file, This XAP file gets copied under client-bin folder.
Now right click on the project and add new item-> Silverlight-> Select silverlightenabled WCF Service.
silverlight-WCF Now we need to define DataContract and Operation Contract for WCF Service.
Create a class to define datacontract and datamembers.
01.[DataContract]
02.   public class  BeyondRelational
03.   {
04.        [DataMember]
05.       public string Name { get; set; }
06.                [DataMember]
07.       public string Country { get; set; }
08.                [DataMember]
09.       public string Location{ get; set; }
10.                [DataMember]
11.       public string Article {get;set; }
12.                [DataMember]
13.       public string Author { get; set; }
14.    }
In the Service1.svc.cs file define operation contract under the service contract
01.public class Service1
02.   {       
03.       [OperationContract]
04.       public List GetAuthors()
05.       {
06.          return new List
07.          {
08.              new BeyondRelational
09.              {
10.                  Country ="India",
11.                   Name = "Hima",
12.                   Location ="hyderbad",
13.                   Article ="SilverlightApplication"
14.              } ,
15.           new BeyondRelational
16.              {
17.                   Country ="USA",
18.                   Name = "Himda",
19.                   Location ="Philly",
20.                  Article ="WPF Application"
21.              },
22.              new BeyondRelational
23.              {
24.                   Country ="Yuganda",
25.                   Name = "Sonia",
26.                   Location ="Efficu",
27.                  Article ="SqlServer 2008 Spacial Data"
28.              }
29.              ,
30.               new BeyondRelational
31.              {
32.                   Country ="Belgium",
33.                   Name = "Sonia",
34.                   Location ="Efficu",
35.                   Article ="WCF Application"
36.              },
37.               new BeyondRelational
38.              {
39.                   Country ="Srilanka",
40.                   Name = "Pinal",
41.                   Location ="Columbo",
42.                   Article ="SharePoint Application"
43.              },
44.               new BeyondRelational
45.              {
46.                   Country ="France",
47.                   Name = "Sonisa",
48.                   Location ="Paris",
49.                   Article ="BING"
50.              }
51.          };
52.       }                      
53.   }
Note: Operation contract attribute can be added to the operations that can be invoked by clients on the service. Service Contract attribute defines the functionality that service exposes.
Build the application.
Now we need to consume this service in silverlight project and display the author’s details in beyond relational.
MainPage.XAML is where we write the code ,that gets integrated to service and calls it dynamically.
This is the silverlightUI
01.<Grid x:Name="LayoutRoot"  Width="400" Height="300" Background="AliceBlue" VerticalAlignment="Center" DataContext="{Binding}">
02.        <Grid.RowDefinitions>
03.            <RowDefinition Height="160" ></RowDefinition>
04.            <RowDefinition></RowDefinition>
05. 
06.        </Grid.RowDefinitions>
07.        <TextBlock x:Name= "NameTextBlocks"  Text="Beyond Relational Author LIST "  VerticalAlignment="top"></TextBlock>
08.        <ComboBox Height="23" HorizontalAlignment="Left"  x:Name="BRComboBox" VerticalAlignment="Top" Width="250" SelectionChanged="BRComboBox_SelectionChanged" DisplayMemberPath="Name" />
09.        <Grid x:Name="BRDetailGrid"  Grid.Row="1" VerticalAlignment="Top">
10.        <Grid.RowDefinitions>
11.                <RowDefinition></RowDefinition>
12.                <RowDefinition></RowDefinition>
13.                <RowDefinition></RowDefinition>
14.                <RowDefinition></RowDefinition>
15.            </Grid.RowDefinitions>
16.            <Grid.ColumnDefinitions>
17.                <ColumnDefinition></ColumnDefinition>
18.                <ColumnDefinition></ColumnDefinition>
19.                <ColumnDefinition></ColumnDefinition>
20.            </Grid.ColumnDefinitions>
21.            <TextBlock x:Name= "NameTextBlock" Grid.Row ="0" Grid.Column="0" FontWeight="Bold" FontFamily="Verdna" Text="Author Name:" HorizontalAlignment="Right" ></TextBlock>
22.            <TextBlock x:Name="NameValue" Grid.Row="0" FontFamily="Verdna" FontWeight="bold" Grid.Column="1" Text="{Binding Name}" HorizontalAlignment="right" />
23.            <TextBlock x:Name= "LocationTextBlock" Grid.Row ="1" Grid.Column="0" FontWeight="Bold" Text="Location:" HorizontalAlignment="Right" ></TextBlock>
24.            <TextBlock x:Name="LocationValue" Grid.Column="1" Grid.Row="1" FontFamily="Verdna" FontWeight="bold" Text="{Binding Location}"  HorizontalAlignment="Right"></TextBlock>
25.            <TextBlock x:Name="CountryTextBlock" Grid.Row="2"   Grid.Column="0" FontFamily="Verdna" FontWeight="bold" Text="Country"  HorizontalAlignment="Right"></TextBlock>
26.            <TextBlock x:Name="CountryValue" Grid.Row="2" Grid.Column="1"  FontFamily="Verdna" FontWeight="bold" Text="{Binding Country}"  HorizontalAlignment="Right"></TextBlock>
27.            <TextBlock x:Name="PriceTextBlock" Grid.Row="3" Grid.Column="0"   FontFamily="Verdna" FontWeight="bold" Text="Article Title"  HorizontalAlignment="Right"></TextBlock>
28.            <TextBlock x:Name="PriceValue"  Grid.Row="3" Grid.Column="1"   FontFamily="Verdna" FontWeight="bold" Text="{Binding Article}"  HorizontalAlignment="Right"></TextBlock>
29.        </Grid>
30.        
31.    </Grid>
Databinding sets the values of objects and controls dynamically.This way we bind from the service { Binding } is used to bind corresponding data dynamically from the service. Calling the service in code behind mainpage.xaml.cs
1.public MainPage()
2.       {
3.           InitializeComponent();
4.           BRService.Service1Client proxy = new BRService.Service1Client();
5.           proxy.GetAuthorsCompleted += new EventHandler(proxy_GetAuthorsCompleted);
6.           proxy.GetAuthorsAsync();
7.       }
Code to dynamically render authors from the dropdownlist selection
1.private void BRComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
2.{
3.    BRDetailGrid.DataContext = (sender as ComboBox).SelectedItem as BRService.BeyondRelational;
4.}
Services need to be accessed asynchronously in Silverlight.
Calling a proxy class event to dynamically
1.void proxy_GetAuthorsCompleted(object sender, Demos.BRService.GetAuthorsCompletedEventArgs e)
2. {
3.     BRComboBox.ItemsSource = e.Result;
4. }
Results
results

0 comments:

Post a Comment