Follow the below steps to know How to use WCF Service to pass the data.
Step - 1 : Open the Visual Studio 2013 Ultimate
Step - 2 : Go to
File ->New ->Project.
Step - 3 : Then, the
New Project
window will appear. Give the project name as
SessionExample.
See the below figure
and make sure that you following the steps marked in the red color in the below figure.
Step - 4 : Next other window will be open. Click on
Ok. After that Solution will be created.
Step - 5 : Go to
Solution Explorer
and then Right click on the web project and then Click on
Add ->New Item as shown in the below figure.
Step - 6 : Then the
Add New Item window will be appear as shown below. Select the
Silverlight and then
Silverlight enabled WCF Service as markd in the below figure. Click on
Add.
Step - 7 : Then, the following code will be displayed in the
Service1.svc.cs.
[csharp]using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace SessionExample.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
}
}
[/csharp]
Step - 8 : Now, add the method as shown below.
[csharp]using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace SessionExample.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public object GetSessionVariable(string key)
{
return System.Web.HttpContext.Current.Session[key];
}
}
}
[/csharp]
Step - 9 :Now, Go to Solution Explorer and Right click on the
Service1.svc and Click on the
Set As Start Page as shown in the below figure.
Step - 10 : press
F5 to run the Application. Then you will get the following window. Copy the Url which marked in the below figure.
Step - 11 : Now, Go to
Solution Explorer and then Right click on the project. Click on the
Add Service Reference as shown in the below figure.
Step - 12 : Paste the URL which was copied before and then press
GO and then give the
Name for the namespace and click on
Ok as shown in the below figure.
Now, we can use our WCF Service in our project.
Step - 12 : Open the MainPage.xaml page and add a Textblock from the toolbox to our form as shown below.
[html]
<UserControl x:Class="SessionExample.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">
<TextBlock x:Name="Textblock1" HorizontalAlignment="Left" Margin="102,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="0.528,-1.051" Height="50" Width="200" FontSize="22"/>
</Grid>
</UserControl> [/html]
Step - 13 : Write the following code in the
MainPage.xaml.cs file.
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SessionExample
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MyService.Service1Client client = new SessionExample.MyService.Service1Client();
client.GetSessionVariableCompleted += new EventHandler(GetSessionVariable);
client.GetSessionVariableAsync("Name");
}
void GetSessionVariable(object sender,
SessionExample.MyService.GetSessionVariableCompletedEventArgs e)
{
if (e.Result == null)
Textblock1.Text = "Session variable is not available";
else
Textblock1.Text = e.Result.ToString();
}
}
}
[/csharp]
Step - 14 : Press F5 to run the application and then you will get the output in the browser.