private static void CallAsyncResult(Task task, IMessageSource source, UInt64 session) { switch (task.Status) { case TaskStatus.Canceled: { source.Respond(new ExceptionMessage(session, new TaskCanceledException())); } break; case TaskStatus.Faulted: { source.Respond(new ExceptionMessage(session, task.Exception.InnerException)); } break; case TaskStatus.RanToCompletion: { Type taskType = task.GetType(); if (taskType.IsGenericType && TypeFormatter.CanSerialize(taskType.GetGenericArguments()[0])) { object result = taskType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance).GetValue(task, null); source.Respond(new ResultMessage(session, result)); } else { source.Respond(new ResultMessage(session, null)); } } break; } }
private static void Call(IMessageSource source, IMessage message, Func <object[], object> target, bool extend) { object[] args; message.TryGetInvocationArgs(out args); if (extend) { args = args.Insert(0, source); } try { object result = target(args); if (result != null && !TypeFormatter.CanSerialize(result.GetType())) { result = null; } source.Respond(new ResultMessage(message, result)); } catch (Exception er) { try { source.Respond(new ExceptionMessage(message, er)); } catch (Exception fatal) { Application.Error(fatal); } } }
private static void CallAsync(IMessageSource source, IMessage message, Func <object[], object> target, bool extend) { UInt64 id; if (message.TryGetTransferId(out id)) { object[] args; message.TryGetInvocationArgs(out args); if (extend) { args = args.Insert(0, source); } try { Task task = target(args) as Task; if (task != null) { task.ContinueWith((tsk) => CallAsyncResult(tsk, source, id)); } else { throw new ContextMarshalException(string.Concat(target.ToString(), " not async or not returning System.Threading.Tasks.Task")); } } catch (Exception er) { try { source.Respond(new ExceptionMessage(message, er)); } catch (Exception fatal) { Application.Error(fatal); } } } else { throw new ContextMarshalException(string.Concat(target.ToString(), " needs a transfer identifier")); } }