In this recipe, let's create a simple web service using WCF and learn how to consume that service using a Phone 7 Client App.
Open the Visual Studio and create a new project from the template WCF Service and name it Recipe1_SimpleService
. Delete the default files Service1.svc
and IService1.cs
from the project.
For this recipe, we will be creating two projects; one is a WCF Service application and the other one is a Windows Phone client application. In the Service application, we will add the service contract and operation contract. Then, we will add a simple method to return the service information. Once the service is built, we will consume that service in the Windows Phone client application to display what is returned by the method.
SimpleService.svc
. The project template automatically adds the ISimpleService.cs
file, as shown in the following screenshot: ISimpleService.cs
file and add two classes, one for the data contract and another for the service contract. The data contract class, ServiceInfo
will have one property named ServiceName
. The service contract, ISimpleService
is an interface class, which will have one function, GetServiceInfo:
namespace Recipe1_SimpleService { [DataContract] public class ServiceInfo { [DataMember] public string ServiceName { get; set; } } // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISimpleService" in both code and config file together. [ServiceContract] public interface ISimpleService { [OperationContract] string GetServiceInfo(); } }
SimpleService.svc.cs
file and add the public
function GetServiceInfo
. This method just returns a string "Simple Service Demo
" when the service is called from the client:public class SimpleService : ISimpleService { public string GetServiceInfo() { return "Simple Service Demo"; } }
Recipe1_SimpleServiceClient
. SimpleServiceReference
. After adding the service, the project should look like the following screenshot: MainPage.xaml
file; let's add a button and a textblock control to display the return string from the service.<!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="Simple Service" Margin="9,- 7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Call Service" Height="72" HorizontalAlignment="Left" Margin="37,63,0,0" Name="button1" VerticalAlignment="Top" Width="379" Click="button1_Click" /> <TextBlock Height="59" HorizontalAlignment="Left" Margin="51,194,0,0" Name="textBlock1" Text="Results" VerticalAlignment="Top" Width="365" /> </Grid>
MainPage.xaml.cs
file and add the using
declarative to the service reference at the beginning of the file:using Recipe1_SimpleServiceClient.SimpleServiceReference;
SimpleServiceClient
in the MainPage
class, as follows:public partial class MainPage : PhoneApplicationPage { // Constructor SimpleServiceClient ssc; public MainPage()
EventHandler
to catch the GetServiceInfoCompleted
event as shown in the following code:public MainPage() { InitializeComponent(); ssc = new SimpleServiceClient(); ssc.GetServiceInfoCompleted += new EventHandler<GetServiceInfoCompletedEventArgs> (ssc_GetServiceInfoCompleted); } void ssc_GetServiceInfoCompleted(object sender, GetServiceInfoCompletedEventArgs e) { if (e.Result != null) { textBlock1.Text = e.Result.ToString(); } }
GetServiceInfoAsync
method:private void button1_Click(object sender, RoutedEventArgs e) { ssc.GetServiceInfoAsync(); }
In this recipe, we created a simple service to understand what it takes to create the service and consume it using a Phone 7 client.
The ISimpleService
file defines the DataContract
and ServiceContract
. Based on this, the client can access the Member
class and property. ServiceContract
is the method available for the client's consumption.
When the service is added to the client application, a method that is defined as ServiceContract
, such as GetServiceInfo
is visible and can be invoked to get the results.
This recipe is a simple demonstration of a service model. In the next couple of recipes we will dive deeper into topics such as connecting to a database and using the Entity Framework to create all the proxy classes.