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.
Right-click the ErrorHandlingDemo solution in Solution Explorer and choose the property. The solution property page Window shown in Figure 4-6 will display.
Select the Multiple startup projects option, and CalculatorService and ErrorHandlingDemo projects' Actions are set to Start.
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).
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.
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.
When you hover over x value you will notice that it contains Test, which cannot be converted to integer causing InvalidCastException.
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 .
Press F5 again, and the execution will break on the line txtAnswer.Text = e.Result.ToString() found in MainPage.xaml.cs.
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.
Press F5 again, and you will notice the exception thrown in the e.Result property, as shown in Figure 9.
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();
}
else
{
MessageBox.Show(
string.Format("CalculatorService return an error {0}",
e.Error.Message));
}
};
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.