Handling exceptions in Silverlight Asynchronous lambda calls -
in silverlight use lambdas data retrieval service (in example wcf data service). exceptions inside callback swallowed system unless handle them try catch. example:
this.context.beginsavechanges(() => { // throwing exception lost , application_unhandledexception doesn't catch }, null);
i have helper function log exception , redirect aspx general error page, have wrap in lambdas try/catches ok, if have it, there better way?
you create set of helper methods wrap lambda's with:-
public static class helper { public static asynccallback getasynccallback(action<iasyncresult> inner) { return (a) => { try { inner(a); } catch (exception err) { // handling "uncaught" errors } }; } public static action getaction(action inner) { return () => { try { inner(); } catch (exception err) { // handling "uncaught" errors } }; } public static action<t> getaction(action<t> inner) { return (a) => { try { inner(a); } catch (exception err) { // handling "uncaught" errors } }; } // , on also:- public static func<t> getfunc(func<t> inner;) { return () => { try { return inner(); } catch (exception err) { // handling "uncaught" errors } }; } public static func<t1, treturn> getfunc(func<t1, treturn> inner;) { return (a) => { try { return inner(a); } catch (exception err) { // handling "uncaught" errors } }; } }
now can wrap lambda's without worrying default boilerplate exception handling:-
this.context.beginsavechanges(helper.getasynccallback((ar) => { // needs specific exception handling or none @ }), null);
Comments
Post a Comment