Beispiel #1
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var name         = binder.Name;
            var descriptor   = _client.GetMethodDescriptor(_descriptor.Name, name, args);
            var asyncAdapter = false;

            if (descriptor == null && name.EndsWith("Async", StringComparison.Ordinal))
            {
                name         = name.Substring(0, name.Length - 5);
                descriptor   = _client.GetMethodDescriptor(_descriptor.Name, name, args);
                asyncAdapter = true;
            }
            if (descriptor == null)
            {
                throw new Exception("Method not found");
            }

            var returnType = descriptor.TypeOfReturnType;
            var isAsync    = returnType == typeof(Task) || returnType.BaseType == typeof(Task);

            if (isAsync)
            {
                var res = _client.ServerInvokeAsync(_descriptor.Name, name, args).WaitAndResults();
                if (returnType.GenericTypeArguments.Length > 0)
                {
                    var taskCreator = TaskFromResultsMethods.GetOrAdd(returnType.GenericTypeArguments[0], type => typeof(Task).GetMethod("FromResult").MakeGenericMethod(type));
                    _taskArgument[0] = res;
                    result           = taskCreator.Invoke(null, _taskArgument);
                    _taskArgument[0] = null;
                }
                else
                {
                    result = Task.CompletedTask;
                }
            }
            else if (asyncAdapter)
            {
                result = _client.ServerInvokeAsync(_descriptor.Name, name, args);
            }
            else
            {
                result = _client.ServerInvokeAsync(_descriptor.Name, name, args).WaitAndResults();
            }
            return(true);
        }
Beispiel #2
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var name         = binder.Name;
            var descriptor   = _client.GetMethodDescriptor(_descriptor.Name, name, args);
            var asyncAdapter = false;

            if (descriptor is null && name.EndsWith("Async", StringComparison.Ordinal))
            {
                name         = name.Substring(0, name.Length - 5);
                descriptor   = _client.GetMethodDescriptor(_descriptor.Name, name, args);
                asyncAdapter = true;
            }
            if (descriptor is null)
            {
                throw new Exception("Method not found");
            }

            var returnType = descriptor.TypeOfReturnType;

            if (descriptor.ReturnIsTask)
            {
                var res = _client.ServerInvokeAsync(_descriptor.Name, name, args).WaitAndResults();
                if (descriptor.CreateTaskFromResult != null)
                {
                    result = descriptor.CreateTaskFromResult(null, new[] { res });
                }
                else
                {
                    result = Task.CompletedTask;
                }
            }
            else if (asyncAdapter)
            {
                result = _client.ServerInvokeAsync(_descriptor.Name, name, args);
            }
            else
            {
                result = _client.ServerInvokeAsync(_descriptor.Name, name, args).WaitAndResults();
            }
            return(true);
        }