Azure for Developers Tutorial Step 4: Calling the WCF service from the client

This is the fourth step of the Azure for Developers tutorial, in which we set up a WCF service running in Azure to provide CRUD operations to a client application. For more information, please check out the Introduction.

In the previous step, we finished the WCF service and ran it in the compute emulator. So if you haven’t got it running, you’ll want to run it now. Put a breakpoint in AddCustomer in CustomerServices.svc.cs. Let’s up the client application. If you haven’t already downloaded the client application we’ll be changing, please do so now; it is here.

The first thing you want to do is add a service reference to your service running in the compute emulator. This will retrieve the service definition. So right-click on Service References and select “Add Service Reference”. Find the open browser showing your service information and copy the link. It will be something like http://127.0.0.1:81/CustomerServices.svc.

Paste the link into the Add Service Reference dialog. Click Go to find the service. If it finds it, it will show it in the Services window. If you click on the service’s interface, it will show the exposed Operation Contracts on the right. When adding new methods to your service, you have to be sure to also add them to the interface, or they won’t be exposed to the client. (Please don’t ask how many times I’ve forgotten this!)

At the bottom, change the Namespace to CustomerSvc and click OK.

We need to set up the Data Access class in the client. Open DAC.cs. First, let’s add a method to get the proxy and set up the endpoint. This enables us to easily programmatically change the address of the service we’re connecting to. We’ll call this from the proxy methods that call the WCF service.

//put these here rather than relying on the app.config being right 
//so you can set up a service reference running the service locally,
//and then just change this to point to the instance in the cloud
private static string m_endpointAddress = @"http://127.0.0.1:81/CustomerServices.svc";
//private static string m_endpointAddress = 
//  http://yourservicename.cloudapp.net/CustomerServices.svc";

private static CustomerSvc.CustomerServicesClient getClient()
{
  CustomerSvc.CustomerServicesClient prx = new CustomerSvc.CustomerServicesClient();
  prx.Endpoint.Address = new System.ServiceModel.EndpointAddress(m_endpointAddress);
  //this sets the timeout of the service call, which should give you enough time
  //  to finish debugging your service call
  prx.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
  return prx;
}

Now let’s add the rest of the methods:

internal static string GetFavoritesForCustomer(out string favoriteMovie, 
  out string favoriteLanguage, string firstName, string lastName)
{
  favoriteMovie = string.Empty;
  favoriteLanguage = string.Empty;
  CustomerSvc.CustomerServicesClient prx = getClient();
  return prx.GetFavorites(out favoriteMovie, out favoriteLanguage, firstName, lastName);
}

internal static string UpdateFavoritesByName(string firstName, string lastName, 
  string favoriteMovie, string favoriteLanguage)
{
  CustomerSvc.CustomerServicesClient prx = getClient();
  return prx.UpdateFavoritesByName(firstName, lastName, favoriteMovie, favoriteLanguage);
}

internal static string GetCustomerList(out DataSet customers)
{
  customers = new DataSet();
  CustomerSvc.CustomerServicesClient prx = getClient();
  return prx.GetCustomerList(out customers);
}

internal static string AddACustomer(string firstName, string lastName, 
  string favoriteMovie, string favoriteLanguage)
{
  CustomerSvc.CustomerServicesClient prx = getClient();
  return prx.AddACustomer(firstName, lastName, favoriteMovie, favoriteLanguage);
}

These are already hooked up in the code-behind in the form. So you can just click F5 to run the application.

So now you should have the service running and the client application running, and you should have a breakpoint in AddCustomer in the service. Fill in the four fields and click on the Add button.

It should call into the service and stop at your breakpoint. Then you can step through your service code and debug it if you have any problems.

Add a few records, then click on GetCustomerList to retrieve them. Fill in a First Name and Last Name from the list and blank out the two Favorite fields, then click GetFavorites, and it should fill them in. Change the favorites and click UpdateFavorites, then retrieve the Customer list again and see if they have been changed.

If everything works, you’re golden. You now have a working WCF service and a client to test it with.

In the next post, we’ll add a queue to the service and add a service call to add a message to the queue. Then we’ll add a worker role to the service that will retrieve messages from the queue and write them to blob storage.

Tags:

3 Responses to “Azure for Developers Tutorial Step 4: Calling the WCF service from the client”

  1. ishita saha Says:

    Hi Rohin, I was following your article “How to Migrate a Web Application to Windows Azure and SQL Azure” and got stuck at step 10 while running my application. its giving the error :Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: Version string portion was too short or too long.

    Source Error:

    Line 1:
    Line 2:
    Line 3: <asp:Content ID="Content1" runat="server"
    with line 1 highlighted. It works fine when I dont run the application as administrator. I am new to this..can you please help?

  2. Azure for Developers: Introduction to the Tutorial | RobinDotNet's Blog Says:

    […] Step 4: Calling the WCF service from the client […]

Leave a reply to robindotnet Cancel reply