Tutorials » Silverlight tutorials »

Create WCF service to retrieve session data - part II


This chapter explains how to create a WCF service to return Session data to Silverlight controls.


This chapter is the continuation of the previous chapter. If you havent read the previous chapter, please read it before you continue here.

Create a new Silverlight project with a web project to host the Silverlight control.

Steps:

1. Open Visual Studio and select the menu "File" > "New" > "Project"

2. Select the Project Type as "Silverlight" under your favorite language and choose the template "Silverlight Application". I have selected Visual C# as the tutorials here. I have named my project as "AccessSessionFromSilverlight" and have selected the option "Create directory for solution" so that all my project files are organized within a folder structure.

3. In the next screen, choose the option "Add a new web to the solution for hosting the control".

4. After Visual Studio creates the solution, right click on the web project in the Solution Explorer and select "Add New Item".

5. Select the category "Silverlight" from the left panel in the dialog box. From the right panel, select the template "Silverlight-enabled WCF Service"

6. Choose the default name "Service1.svc" and press the "Add" button.

7. You can see the below code:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public void DoWork()
{
return;
}
}


Add another mthod as shown below:

[OperationContract]
public object GetSessionVariable(string key)
{
return System.Web.HttpContext.Current.Session[key];
}


8. Open your solution explorer. Right click on the file "Service1.svc" and select "Set as Startup Page".

9. Now run the application by pressing Ctrl + F5 (or, using the menu "Debug" > "Start Without Debugging")

The browser will be opened and Service1 meta data will be displayed. Ignore the content in the page, but copy the URL from the browser. The URL will look something like this:
http://localhost:1873/Service1.svc
The port number may be different in your case, but that is OK.

10. Return to your Solution Explorer in Visual Studio. Right click on your Silverlight project (not web project) and select "Add Service Reference".
In the field for Address, specify the URL you copied from the browser and press "GO".
In the bottom left, change the namespace to "MySampleService".
Press "OK" to add the service reference.

Now you can call our WCF service from the Silverlight client.

To test this, follow these steps:

1. Open the Page1.xaml file and add a textblock to display the string returned from the WCF method call.

2. Add an event handler for the "Loaded" event of the UserControl.

Complete code from Page1.xaml is given below:

<UserControl x:Class="Silverlight_With_WCF.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300"
Loaded="UserControl_Loaded">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="textblock1" Width="200" Height="30"></TextBlock>
</Grid>
</UserControl>


Now go to the code behind file (Page1.xaml.cs) and add the WCF service call as shown below:


private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MyService.Service1Client client = new AccessSessionFromSilverlight.MyService.Service1Client();
client.GetSessionVariableCompleted += new EventHandler(client_GetSessionVariableCompleted);
client.GetSessionVariableAsync("Name");
}

void client_GetSessionVariableCompleted(object sender,
AccessSessionFromSilverlight.MyService.GetSessionVariableCompletedEventArgs e)
{
if (e.Result == null)
textblock1.Text = "Session variable not set";
else
textblock1.Text = e.Result.ToString();
}


Open the web project in Solution Explorer and set the Silverlight test page as the startup page. Now run the project. You can see that the WCF service is called from Silverlight client and the result is displayed in the browser.

You can download a sample project for this tutorial.




Next Chapter: How to call Javascript functions from Silverlight code ?
Previous Chapter: Access Session variables from Silverlight controls
More Chapters: Silverlight Tutorials
More Tutorials: Tutorial Index



Top Contributors
TodayLast 7 Daysmore...

Awards & Gifts

Online Members

Manigandan
More...
 
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India