public Task <IResponse> InvokeTaskAsync(ServiceInvoke info) { var tcs = new TaskCompletionSource <IResponse>(); Action <IResponse> handler = null; handler = (result) => { if (result.Id == info.Id) { this.Invoked -= handler; tcs.TrySetResult(result); } }; //Timer t = null; //t = new Timer(_ => { // this.InvokeCompleted -= handler; // tcs.TrySetCanceled(); // t.Dispose(); //}); //t.Change(5000, Timeout.Infinite); this.Invoked += handler; this.InvokeAsync(info); return(tcs.Task); }
public object Invoke(ServiceInvoke info) { var task = InvokeTaskAsync(info); while (!task.IsCompleted) { SynchronizationContext ctx = SynchronizationContext.Current; if (ctx != null) { if (ctx is WindowsFormsSynchronizationContext) { Application.DoEvents(); } } //Console.WriteLine("IsCompleted"); Thread.Sleep(1); } var result = task.Result; if (result.Success) { var invokeResult = result as ServiceInvokeResult; return(invokeResult.ReturnValue); } throw new InvalidOperationException(result.Message); }
protected override void OnInvokeMethod(InvocationContext context) { var method = context.Method as MethodInfo; ServiceInvoke info = new ServiceInvoke { Id = Guid.NewGuid(), Name = ServiceName, Action = context.Method.Name, Arguments = context.Arguments, Instance = Instance, DestUserId = UserId, }; Type returnType = method.ReturnType; if (typeof(Task).IsAssignableFrom(returnType)) { Type type = returnType.GetGenericArguments()[0]; context.Return = ServiceClientHandler.GetType() .GetMethod("InvokeTaskAsync2") .MakeGenericMethod(type) .Invoke(ServiceClientHandler, new object[] { info }); } else { context.Return = ServiceClientHandler.Invoke(info); } }
protected virtual ServiceInvokeResult Invoke(ServiceInvoke info) { ServiceInvokeResult result = new ServiceInvokeResult(); result.Id = info.Id; result.DestUserId = info.SrcUserId; result.SrcUserId = info.DestUserId; result.Instance = info.Instance; object service = GetService(info.Name, info.Instance); if (service == null) { result.Message = "服务不存在"; return(result); } MethodInfo method = null; try { method = service.GetType().GetMethod(info.Action); if (method == null) { result.Message = "方法不存在"; return(result); } } catch { result.Message = "查找方法出错"; return(result); } object ret; try { ret = method.Invoke(service, info.Arguments); } catch (Exception ex) { ex = ex.InnerException; result.Message = "调用出错"; return(result); } Task task = ret as Task; if (task != null) { if (task.IsCompleted) { ret = task.GetType().GetProperty("Result").GetValue(task); } //TODO 等待完成 } result.Success = true; result.ReturnValue = ret; return(result); }
public async Task <T> InvokeTaskAsync2 <T>(ServiceInvoke info) { var result = await InvokeTaskAsync(info); if (result.Success) { var invokeResult = result as ServiceInvokeResult; return((T)invokeResult.ReturnValue); } throw new InvalidOperationException(result.Message); }
public override async void Handle(HandleContext context) { var packet = context.Packet; var session = context.Session; ServiceInvoke info = packet as ServiceInvoke; if (info != null) { context.Cancel = true; Console.WriteLine("收到调用{0}.{1} {2}->{3}", info.Name, info.Action, info.SrcUserId, info.DestUserId); var result = Invoke(info); await session.SendAsync(result); } else { Subscribe sub = packet as Subscribe; if (sub != null) { context.Cancel = true; Console.WriteLine("收到事件注册"); object service = GetService(sub.Name, sub.Instance); var e = service.GetType().GetEvent(sub.Action); //AddEventHandler(sub.Name, service); } } }
public async void InvokeAsync(ServiceInvoke info) { Console.WriteLine("发起调用{0}.{1}", info.Name, info.Action); await session.SendAsync(info); }