Writing and consuming a simple web service

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.

Getting ready

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.

Getting ready

How to do it...

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.

  1. Right-click on the project and add a new item using the project template WCF Service and name this file SimpleService.svc. The project template automatically adds the ISimpleService.cs file, as shown in the following screenshot:
    How to do it...
  2. Open the 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();
    }
    }
    
  3. Now open the 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";
    }
    }
    
  4. Press F5, and then build and run the service. You should be presented with a WCF Test Client, which is an easy way to test the service. Select the ISimpleService and then click on GetServiceInfo and you will get an option to Invoke. When you invoke the service method you should see the request and response in XML formats, as shown in the following screenshot:
    How to do it...
  5. We just created a simple WCF service, which can be hosted in the web server environment or we can run it in the localhost so we can test the service with a client application.
  6. Now let's build a client Phone 7 application to consume this service. Open the new project and create a Phone 7 Application. Name it Recipe1_SimpleServiceClient.
  7. Right-click on the References folder and add Service Reference to the project. Type in the localhost service URI into the address textbox and click on Go. This will create all the proxy classes in the client application to help make calls to the WCF Service. Also, type the Reference name as SimpleServiceReference. After adding the service, the project should look like the following screenshot:
    How to do it...
  8. Open the 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>
    
  9. Open the MainPage.xaml.cs file and add the using declarative to the service reference at the beginning of the file:
    using Recipe1_SimpleServiceClient.SimpleServiceReference;
    
  10. Add an instance of the context object for SimpleServiceClient in the MainPage class, as follows:
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    SimpleServiceClient ssc;
    public MainPage()
    
  11. Add an 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();
    }
    }
    
  12. Now add the button click event and call the GetServiceInfoAsync method:
    private void button1_Click(object sender, RoutedEventArgs e)
    {
    ssc.GetServiceInfoAsync();
    }
    
  13. Press F5 , and when you click on the Call Service button you should get the service info string from the WCF service:
    How to do it...

How it works...

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.

There's more...

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.

See also

Check the recipe Using LINQ to SQL for creating the service in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset