Windows Phone 7 Development : Debugging Application Exceptions (part 2) - Debugging a Web Service Exception

1/19/2011 11:35:48 AM

2. Debugging a Web Service Exception

ErrorHandlingDemo contains the CalculatorService web service project, where the service will be hosted locally and consumed by the demo application. The code is written so that the application will throw the exceptions that you will be fixing.

2.1. Catching a Web Service Exception

You will be stepping through the breakpoints in order to understand the behavior of the thrown exception.

Before you begin, we need to make sure that both the Windows Phone project and the web service project start simultaneously when you Press F5.

  1. Right-click the ErrorHandlingDemo solution in Solution Explorer and choose the property. The solution property page Window shown in Figure 4-6 will display.

  2. Select the Multiple startup projects option, and CalculatorService and ErrorHandlingDemo projects' Actions are set to Start.

  3. Also put two breakpoints in MainPage.xaml.cs, as shown in Figure 6, at the line txtAnswer.Text = e.Result.ToString() and _svc.AddAsync(txtX.Text, txtY.Text).

    Figure 6. Breaking point to debug CalculatorService web service
  4. Press F5 and you will see the application show in Figure 1 in the emulator, and you will notice the WCF Test Client starts as well, as shown in Figure 7. The WCF Test Client will host the CalculatorService, allowing you to step into the web service call.

    Figure 7. WCF test client
  5. From the emulator, press the Call Calculator Service button.

Notice that the Visual Studio catches InvalidCastException thrown from the CalculatorService project, as shown in Figure 8.

Figure 8. InvalidCastException in CalculatorService

  1. When you hover over x value you will notice that it contains Test, which cannot be converted to integer causing InvalidCastException.

  2. Press F5 to continue, and Visual Studio breaks at Reference.cs, which is the web service proxy class that was generated against WSDL from Visual Studio .

  3. Press F5 again, and the execution will break on the line txtAnswer.Text = e.Result.ToString() found in MainPage.xaml.cs.

  4. In Immediate Window, type in e.Error and you will notice that e.Error is not empty. When the web service returns any kind of error, e.Error will not be empty, and when you try to access e.Result that contains web service call, the result will throw an exception.

  5. Press F5 again, and you will notice the exception thrown in the e.Result property, as shown in Figure 9.

    Figure 9. Exception thrown in e.Result when the web service has an error
  6. Press F5, and the exception will be finally caught in Application_UnhandledException.

2.2. Fixing the CalculatorService Exception

After stepping through the breakpoints, you now have enough information to fix the exception.

First let's check the values received from the caller in CalculatorService. Replace Service1.svc.cs codes with the following snippet. The CheckValue method will make sure that the received value is not null and it converts the value to the integer.

public int Add(object x, object y)
int xValue = CheckValue(x);
int yValue = CheckValue(y);

return xValue + yValue;

private int CheckValue(object value)
int convertedValue = −1;
if (value == null)
throw new ArgumentNullException("value");
else if (!int.TryParse(value.ToString(), out convertedValue))
throw new ArgumentException(
string.Format("The value '{0}' is not an integer.", value));

return convertedValue;

In MainPage.xaml.cs replace the AddCompleted event delegate with following codes. You will be checking to make sure e.Error is empty before retrieving e.Result, and if e.Error is not empty, then you will be throwing the proper error message.

_svc.AddCompleted += (s, e) =>
if (e.Error == null)
txtAnswer.Text = e.Result.ToString();
string.Format("CalculatorService return an error {0}",

3. Testing the Application

You've finished debugging and fixing the application exceptions, and now you will be able to properly run the application and handle exceptions.

Press F5 and you will see Figure 4-1; notice now that txtDeviceManufacturer and txtDeviceName are properly populated during the MainPage load. When you change txtX to an integer and click the Call Calculator Service button, txtAnswer will be populated with the result received from the web service.

  •  Programming the Mobile Web : JavaScript Libraries
  •  Programming the Mobile Web : Ajax Support
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 5) - Deploying the Service to Windows Azure
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 4) - Coding NotepadViewModel
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 2) - Coding MainPage
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 1) - Building the User Interface
  •  Building Android Apps : Detecting Browsers with WURFL
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
    Most View
    Fujifilm XF1 - Compact Camera With Retro Design
    Late 2012 Razer Blade - One Of The Luxurious Laptops (Part 1)
    Intel’s Next Unit Of Computing Review - The Future Desktop Or Another Nettop? (Part 4)
    The Dell XPS Duo 12 Hybrid Ultrabook - A Swiveling Good Start
    Microsoft Dynamic AX 2009 : Configuration and Security - Security Framework (part 3) - Security Coding
    What Can You Buy For Under $160? (Part 1)
    Nvidia GeForce GTX 760 2 GB Graphic Card Review (Part 3)
    Netgear WNDR4500 - Delivers The Fastest Throughput
    Asus Zenbook Prime UX51Vz – An Attractive And Sophisticated Ultrabook (Part 2)
    Samsung WB250F – A Camera With Built-In Wi-Fi (Part 1)
    Top 10
    Olympus Stylus 1 - The Pied Piper (Part 3)
    Olympus Stylus 1 - The Pied Piper (Part 2)
    Olympus Stylus 1 - The Pied Piper (Part 1)
    Olympus Pen E-PL5 - April 2014
    Nikon 1 J3 – April 2014
    New Camera For You – Nikon 1 AW1
    Phase One IQ250, Hasselblad H5D-50c - Medium-format Media Systems: Bigger Gets Better
    Kaveri APU - AMD A10-7700K
    Fujifilm X-M1 – Review April 2014
    Fujifilm X-T1 : Good To Go