Exemple #1
0
        private async void LeaveSession()
        {
            await _transporter.SendAsync(new LeaveSessionRequest()
            {
            });

            InSession = false;
        }
        protected override bool TryInvokeMember(Type interfaceType, MethodInfo methodInfo, object[] args, out object result)
        {
            var attr = methodInfo.GetRpcAttribute();

            if (methodInfo.GetParameters().Length > 1)
            {
                var list = new MethodParamCollection();

                foreach (var arg in args)
                {
                    list.Add(arg);
                }

                args = new object[] { list };
            }

            if (methodInfo.ReturnType == typeof(void) || methodInfo.ReturnType == typeof(Task))
            {
                ResonanceMessageConfig config = new ResonanceMessageConfig();

                if (attr != null)
                {
                    config = attr.ToMessageConfig();
                }
                else
                {
                    config.RequireACK = true;
                }

                config.RPCSignature = RPCSignature.FromMemberInfo(methodInfo);

                if (methodInfo.ReturnType == typeof(Task))
                {
                    result = _transporter.SendAsync(args.FirstOrDefault(), config);
                }
                else
                {
                    _transporter.Send(args.FirstOrDefault(), config);
                    result = new object();
                }
            }
            else
            {
                ResonanceRequestConfig config = new ResonanceRequestConfig();

                if (attr != null)
                {
                    config = attr.ToRequestConfig();
                }

                config.RPCSignature = RPCSignature.FromMemberInfo(methodInfo);

                if (typeof(Task).IsAssignableFrom(methodInfo.ReturnType))
                {
                    var returnType = methodInfo.ReturnType.GetGenericArguments()[0];

                    if (returnType.IsPrimitive)
                    {
                        if (_sendRequestMethod == null)
                        {
                            _sendRequestMethod = typeof(IResonanceTransporter).GetMethods().First(x => x.Name == nameof(IResonanceTransporter.SendRequestAsync) && x.ContainsGenericParameters);
                        }

                        MethodInfo generic = _sendRequestMethod.MakeGenericMethod(typeof(object), returnType);

                        result = generic.Invoke(_transporter, new object[]
                        {
                            args.FirstOrDefault(),
                            config
                        });
                    }
                    else
                    {
                        result = _transporter.SendRequestAsync(args.FirstOrDefault(), config);
                    }
                }
                else
                {
                    result = _transporter.SendRequest(args.FirstOrDefault(), config);
                }
            }

            return(true);
        }