예제 #1
0
        public void Intercept(IInvocation invocation)
        {
            var jsonRpcMethodInfo = invocation.Method.GetCustomAttribute <JsonRpcMethodAttribute>() as IJsonRpcMethodInfo;

            if (jsonRpcMethodInfo == null)
            {
                throw new InvalidOperationException();
            }

            var delegateType = this.GetDelegateType(invocation);

            switch (delegateType)
            {
            case MethodAsyncType.AsyncFunction:
                if (jsonRpcMethodInfo.IsNotification)
                {
                    throw new NotSupportedException();
                }
                var resultType         = invocation.Method.ReturnType.GetGenericArguments()[0];
                var invokeGenericAsync = _invokeAsyncGenericMethodInfo.MakeGenericMethod(resultType);
                var genericTask        = invokeGenericAsync.Invoke(_rpcClient, new object[] { _serviceInfo.Endpoint, jsonRpcMethodInfo.Name, invocation.Arguments });
                var asyncMi            = _handleAsyncMethodInfo.MakeGenericMethod(resultType);
                invocation.ReturnValue = asyncMi.Invoke(this, new[] { genericTask });
                break;

            case MethodAsyncType.AsyncAction:
                if (jsonRpcMethodInfo.IsNotification)
                {
                    var task = _rpcClient.InvokeNotificationAsync(_serviceInfo.Endpoint, jsonRpcMethodInfo.Name, invocation.Arguments);
                    invocation.ReturnValue = HandleAsync(task);
                }
                else
                {
                    var task = _rpcClient.InvokeActionAsync(_serviceInfo.Endpoint, jsonRpcMethodInfo.Name, invocation.Arguments);
                    invocation.ReturnValue = HandleAsync(task);
                }
                break;

            default:
                throw new NotSupportedException();
            }
        }