Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Portable Class Library WCF Async/Completed Exception Handling

$
0
0

Hello,

I'm building a Portable Class Library that wraps a WCF Service Reference and targets .NET 4.5, Windows Store, Windows Phone.

I have hit somewhat of a roadblock, the generated proxy classes are using the async pattern <MethodName>Async(...) methods and <MethodName>Completed(...) events.  While it is claimed that the exception will be forwarded to the Completed event should an error happen this is simply not the case.  During debugging or running without the debugger the exception is classed as unhandled and pops out of the generated EndXxx( ) method thus I cannot catch the exceptions and forward them to the client application (to display an appropriate message and continue execution).

The Completed event is not triggered until after the exception shows up in the debugger as unhandled.  What can I do to catch the exceptions in my Portable Class Library so that I might handle them properly?  Is there something I'm missing?

Thank you!

Here's a cut down version of the code that makes a call to the WCF service client that I expect to throw an exception (for testing purposes).

public static Task<T> TryOperationAsync<TClient, T>(TClient client, Func<TClient, T> operation)
	where TClient : class, ICommunicationObject
{
	return Task.Factory.StartNew(( ) =>
	{
		try
		{
			T result = operation.Invoke(client);
			client.Close( );
			return result;
		}
		catch (Exception ex)
		{
			client.Abort( );
			throw ex;
		}

		return default(T);
	});
}

public async Task<IEnumerable<DataObject>> LoadDataAsync( )
{
	return await SharedServiceHelper.TryOperationAsync(serviceClient, client =>
	{
		ManualResetEvent waitHandle = new ManualResetEvent(false);
		Exception error = null;
		client.GetDataCompleted += (sender, e) =>
			{
				error = e.Error;
				try { cache = e.Result; } catch { }
				((ManualResetEvent)e.UserState).Set( );
			};

		// block this operation until the completed event is fired..
		// this effectively the same as calling await SomeOperationAsync(...)
		waitHandle.Reset( );
		client.GetDataAsync(waitHandle);
		waitHandle.WaitOne( );

		if (error != null) throw error;
		return clientCache;
	});
}





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>