コード例 #1
0
ファイル: ApiWrapper.cs プロジェクト: kinglionsoft/NetRpc
        private static MethodInfo GetMethodInfo(ActionInfo action, object instance)
        {
            var instanceType = instance.GetType();

            foreach (var item in instanceType.GetInterfaces())
            {
                var found = item.GetMethods().FirstOrDefault(i => i.GetFullMethodName() == action.FullName);
                if (found != null)
                {
                    var retM = instanceType.GetMethod(found.Name);
                    if (action.GenericArguments.Length > 0)
                    {
                        var ts = action.GenericArguments.ToList().ConvertAll(Type.GetType).ToArray();
                        // ReSharper disable once PossibleNullReferenceException
                        retM = retM.MakeGenericMethod(ts);
                    }
                    return(retM);
                }
            }

            return(null);
        }
コード例 #2
0
        /// <exception cref="TypeLoadException"></exception>
        public static (MethodInfo instanceMethodInfo, ContractMethod contractMethod, Instance instance) GetMethodInfo(ActionInfo action, List <Instance> instances)
        {
            foreach (var i in instances)
            {
                (MethodInfo instanceMethodInfo, ContractMethod contractMethod) = GetMethodInfo(action, i.Contract, i.Target);
                if (instanceMethodInfo != default)
                {
                    return(instanceMethodInfo, contractMethod, i);
                }
            }

            throw new MethodNotFoundException($"{action.FullName} not found in instances");
        }
コード例 #3
0
        private static (MethodInfo instanceMethodInfo, ContractMethod contractMethod) GetMethodInfo(ActionInfo action, ContractInfo contract, object instance)
        {
            var methodObj = contract.Methods.FirstOrDefault(i => i.MethodInfo.ToFullMethodName() == action.FullName);

            if (methodObj != null)
            {
                var instanceMethodInfo = instance.GetType().GetMethod(methodObj.MethodInfo.Name) !;
                if (action.GenericArguments.Length > 0)
                {
                    var ts = action.GenericArguments.ToList().ConvertAll(Type.GetType).ToArray();
                    // ReSharper disable once PossibleNullReferenceException
                    instanceMethodInfo = instanceMethodInfo.MakeGenericMethod(ts !);
                }

                return(instanceMethodInfo, methodObj);
            }

            return(default);
コード例 #4
0
        public static (MethodInfo instanceMethodInfo, ContractMethod contractMethod) GetMethodInfo(ActionInfo action, List <Contract> contracts, IServiceProvider serviceProvider)
        {
            foreach (var contract in contracts)
            {
                var found = GetMethodInfo(action, contract, serviceProvider);
                if (found != default)
                {
                    return(found);
                }
            }

            throw new MethodNotFoundException($"{action.FullName} not found in instanceTypes");
        }
コード例 #5
0
        /// <exception cref="TypeLoadException"></exception>
        public static (MethodInfo instanceMethodInfo, ContractMethod contractMethod, Instance instance) GetMethodInfo(ActionInfo action, List <Instance> instances,
                                                                                                                      IServiceProvider serviceProvider)
        {
            foreach (var i in instances)
            {
                var found = GetMethodInfo(action, i.Contract, serviceProvider);
                if (found != default)
                {
                    return(found.instanceMethodInfo, found.contractMethod, i);
                }
            }

            throw new MethodNotFoundException($"{action.FullName} not found in instances");
        }
コード例 #6
0
ファイル: OnceCallTask.cs プロジェクト: kinglionsoft/NetRpc
        public Task <T> CallAsync(Dictionary <string, object> header, ActionInfo action, Action <object> callback, CancellationToken token, Stream stream,
                                  params object[] args)
        {
            var tcs = new TaskCompletionSource <T>();
            var t   = Task.Run(async() =>
            {
                _convert.End += (s, e) =>
                {
                    _connection.Dispose();
                };

                _convert.ResultStream += (s, e) =>
                {
                    var reciStream = _convert.GetRequestStream(e.StreamLength);
                    SetStreamResult(tcs, reciStream);
                };

                _convert.CustomResult += (s, e) =>
                {
                    if (e.Value.HasStream)
                    {
                        var reciStream       = _convert.GetRequestStream(e.Value.StreamLength);
                        var resultWithStream = e.Value.Result.SetStream(reciStream);
                        SetStreamResult(tcs, resultWithStream);
                        return;
                    }

                    SetResult(tcs, e.Value.Result);
                };

                _convert.Callback += (s, e) =>
                {
                    callback.Invoke(e.Value);
                };

                _convert.Fault += (s, e) =>
                {
                    SetFault(tcs, e.Value);
                };

                try
                {
                    //Send cmd
                    OnceCallParam p = new OnceCallParam(header, action, stream.GetLength(), args);
                    if (token.IsCancellationRequested)
                    {
                        SetCancel(tcs);
                        return;
                    }

                    await _convert.SendCmdAsync(p);
                    _reg = token.Register(async() =>
                    {
                        try
                        {
                            await _convert.SendCancelAsync();
                        }
                        catch (Exception e)
                        {
                            SetFault(tcs, e);
                        }
                    });

                    //timeout
#pragma warning disable 4014
                    Task.Delay(_timeoutInterval, _timeOutCts.Token).ContinueWith(i =>
#pragma warning restore 4014
                    {
                        SetFault(tcs, new TimeoutException($"Service is not response over {_timeoutInterval} ms, time out."));
                    }, _timeOutCts.Token);

                    //Continue send stream
                    if (stream != null)
                    {
                        await Helper.SendStreamAsync(_convert.SendBufferAsync, _convert.SendBufferEndAsync, stream, token);
                    }
                }
                catch (Exception e)
                {
                    SetFault(tcs, e);
                }
            }, token);

            if (t.IsCanceled)
            {
                tcs.TrySetCanceled();
            }

            return(tcs.Task);
        }