示例#1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual Task InvokeAsync(ClientContext context)
        {
            //var argumentType = request.Action.ArgumentType;
            //var returnType = request.Action.ResultType;

            var sendTask = SendAsync(context);

            #if DEBUG && LogDuration
            var duration0 = stopwatch1.GetAndRest();
            #endif

            var task = sendTask.ContinueWith(tsk =>
            {
            #if DEBUG && LogDuration
                var duration1 = stopwatch1.GetAndRest();
            #endif
                if (tsk.Exception != null)
                    throw tsk.Exception.InnerException;

                var resultMessage = tsk.Result;
                if (resultMessage == null)
                    throw new ClientException("get service data error");

                context.Result = GetResult(resultMessage, context.Action.ResultType, context.Formatter);
            });

            return task;
        }
示例#2
0
 public void OnInvoking(ClientContext context)
 {
 }
示例#3
0
 public void OnInvoked(ClientContext context)
 {
 }
示例#4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual Task<IResponseMessage> SendAsync(ClientContext request)
        {
            var mime = request.Formatter.SupportMimes.First();
            var headDic = new Dictionary<string, string>
            {
                { "Content-Type", mime },
                { "Accept", mime },
            };

            var content = new MemoryStream();
            if (request.Action.ArgumentType != null)
            {
                request.Monitor?.OnSerializing(request);
                try
                {
                    request.Formatter.Serialize(content, request.Request, request.Action.ArgumentType);
                }
                catch (Exception ex)
                {
                    throw new SerializeRequestException("Serialize Request Object failed.", ex);
                }
                finally
                {
                    request.Monitor?.OnSerialized(request);
                }
            }
            content.Position = 0;

            #if DEBUG && LogDuration
            var stopwatch1 = Stopwatch.StartNew();
            #endif

            var sendTask = SendAsync(request.Action.Name, content, headDic);
            return sendTask;
        }