Exemplo n.º 1
0
        /// <exception cref="TypeLoadException"></exception>
        public static ApiContext Convert(ServiceCallParam scp, object[] instances)
        {
            (MethodInfo method, object instance) = GetMethodInfo(scp.Action, instances);
            var ps   = method.GetParameters();
            var args = GetArgs(ps, scp.Args, scp.Callback, scp.Token, scp.Stream);

            return(new ApiContext(scp.Header, instance, method, args));
        }
Exemplo n.º 2
0
        private async Task <ServiceCallParam> GetServiceCallParamAsync()
        {
            var onceCallParam = await _convert.GetOnceCallParamAsync();

            var stream = _convert.GetRequestStream(onceCallParam.StreamLength);
            ServiceCallParam serviceCallParam = new ServiceCallParam(onceCallParam,
                                                                     async i => await _convert.SendCallbackAsync(i, onceCallParam.Action, onceCallParam.Args),
                                                                     _serviceCts.Token, stream);

            return(serviceCallParam);
        }
Exemplo n.º 3
0
        public async Task HandleRequestAsync()
        {
            ServiceCallParam scp = await GetServiceCallParamAsync();

            object ret;

            try
            {
                var apiContext = ApiWrapper.Convert(scp, _instances);
                ret = await _middlewareRegister.InvokeAsync(new MiddlewareContext(apiContext));
            }
            catch (Exception e)
            {
                //send fault
                await _convert.SendFaultAsync(e, scp.Action, scp.Args);

                return;
            }

            var hasStream = ret.TryGetStream(out Stream retStream);

            //send result
            await _convert.SendResultAsync(new CustomResult(ret, retStream.GetLength()), scp.Action, scp.Args);

            //send stream
            if (hasStream)
            {
                try
                {
                    await Helper.SendStreamAsync(i => _convert.SendBufferAsync(i), () =>
                                                 _convert.SendBufferEndAsync(), retStream, scp.Token);
                }
                catch (TaskCanceledException)
                {
                    await _convert.SendBufferCancelAsync();
                }
                catch (Exception)
                {
                    await _convert.SendBufferFaultAsync();
                }
            }
        }