Пример #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));
        }
Пример #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);
        }
Пример #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();
                }
            }
        }