0
votes

I am developing a WP8 application. I created a web service on out-systems and then I am calling those web service methods in my app:

ServiceReference1.WebServiceClient ws = new WebServiceClient();
try
{
 ws.FetchInboxAsync(EmailId);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}

Now if the server is down, I expect the control to go into the catch block but it does not and I get the following exception:

An exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.ni.dll but was not handled in user code.

I do realize that the web service call method is asynchronous, so its exception would not be caught in try catch. On forums, people suggest using await keyword. But when I write

await ws.FetchInboxAsync(EmailId);

I get an error : Cannot await void.

I tried something mentioned in answers here, but still I get the same exception

2

2 Answers

3
votes

You can subscribe to FetchInboxCompleted event:

ServiceReference1.WebServiceClient ws = new WebServiceClient();
ws.FetchInboxCompleted += new EventHandler<ServiceReference1.FetchInboxCompletedEventArgs>(c_FetchInboxCompleted);
ws.FetchInboxAsync(EmailId);

And in event handler, check the result:

static void c_FetchInboxCompleted(object sender, serviceReference1.FetchInboxCompletedEventArgs e)
{
     // check e.Error which contains the exception, if any
}
3
votes

If the auto-generated WCF client proxy supports it, you should be able to await a method ending with TaskAsync:

await ws.FetchInboxTaskAsync(EmailId);

If the auto-generated WCF client proxy doesn't define this method, then you can define it yourself as described on MSDN:

public static Task FetchInboxTaskAsync(this ServiceReference1.WebServiceClient client, string emailId)
{
  return Task.Factory.FromAsync(client.BeginFetchInbox, client.EndFetchInbox, emailId, null);
}