Esempio n. 1
0
        /// <summary>
        /// Invoke remote method and get the result.
        /// </summary>
        /// <typeparam name="T">The return type.</typeparam>
        /// <param name="serviceName">The name of the service</param>
        /// <param name="methodName">The method name to call.</param>
        /// <param name="args">The parameters of the method.</param>
        /// <param name="cancellationToken">The cancellation token which can cancel this method.</param>
        /// <returns>The result value.</returns>
        public async Task <T> InvokeAsync <T>(string serviceName, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            var id          = Interlocked.Increment(ref _requestId);
            var request     = new JsonRpcRequest(id, methodName, new JsonRpcRequestParameter(RequestParameterType.Object, args));
            var requestData = await JsonRpcCodec.EncodeRequestsAsync(new[] { request }, cancellationToken).ConfigureAwait(false);

            var responseData = await ProcessAsync(serviceName, requestData, cancellationToken).ConfigureAwait(false);

            var responses = await JsonRpcCodec.DecodeResponsesAsync(responseData, cancellationToken).ConfigureAwait(false);

            if (responses.Length > 0)
            {
                var response   = responses[0];
                var responseId = Convert.ToInt32(response.Id);
                if (responseId != id)
                {
                    throw new InvalidOperationException("Response id is not matched.");
                }
                if (response.Result is RpcException exception)
                {
                    throw exception;
                }

                var resultString = (string)response.Result;
                using var utf8StringData = Utf8StringData.Get(resultString);
                return(await JsonSerializer.DeserializeAsync <T>(utf8StringData.Stream, JsonRpcConvertSettings.SerializerOptions, cancellationToken).ConfigureAwait(false));
            }

            throw new InvalidOperationException("Fail to get invoke result from server.");
        }
Esempio n. 2
0
        /// <summary>
        /// Invoke remote method without result.
        /// </summary>
        /// <param name="serviceName">The name of the service</param>
        /// <param name="methodName">The method name to call.</param>
        /// <param name="cancellationToken">The cancellation token which can cancel this method.</param>
        /// <param name="args">The parameters of the method.</param>
        /// <returns>Void</returns>
        public async Task VoidInvokeAsync(string serviceName, string methodName, CancellationToken cancellationToken, params object[] args)
        {
            var id          = Interlocked.Increment(ref _requestId);
            var request     = new JsonRpcRequest(id, methodName, new JsonRpcRequestParameter(RequestParameterType.Object, args));
            var requestData = await JsonRpcCodec.EncodeRequestsAsync(new[] { request }, cancellationToken).ConfigureAwait(false);

            var responseData = await ProcessAsync(serviceName, requestData, cancellationToken).ConfigureAwait(false);

            var responses = await JsonRpcCodec.DecodeResponsesAsync(responseData, cancellationToken).ConfigureAwait(false);

            if (responses.Length > 0)
            {
                var response   = responses[0];
                var responseId = Convert.ToInt32(response.Id);
                if (responseId != id)
                {
                    throw new InvalidOperationException("Response id is not matched.");
                }
                if (response.Result is RpcException exception)
                {
                    throw exception;
                }
                var resultString = (string)response.Result;
                if (resultString != "null")
                {
                    throw new InvalidOperationException("The result from server is not [null]");
                }
            }
            else
            {
                throw new InvalidOperationException("Fail to get invoke result from server.");
            }
        }