Ejemplo n.º 1
0
        /// <summary>
        /// レスポンスの型を変換したインスタンスを生成します。
        /// </summary>
        /// <typeparam name="TConvert"></typeparam>
        /// <returns></returns>
        public GrpcResult <TConvert> ConvertResponse <TConvert>(Converter <TResponse, TConvert> converter)
        {
            GrpcResult <TConvert> obj = new GrpcResult <TConvert>(this);

            obj.m_Response = converter(this.m_Response);

            return(obj);
        }
        /// <summary>
        /// 指定された例外を処理し、実行結果を返します。
        /// </summary>
        /// <typeparam name="TResponse">レスポンスの型</typeparam>
        /// <param name="call">呼び出しオブジェクト</param>
        /// <param name="ex">例外</param>
        /// <returns>実行結果</returns>
        private static GrpcResult <TResponse> HandleResponseException <TResponse>(AsyncUnaryCall <TResponse> call, Exception ex)
        {
            Exception actual = GrpcExceptionUtility.GetActualException(ex);

            GrpcCallState state;

            if (GrpcCallInvokerContext.TryGetState(call, out state))
            {
                GrpcExceptionListener.NotifyCatchClientException(state.Method, state.Host, state.Options, actual);
            }

            return(GrpcResult.Create <TResponse>(actual));
        }
 /// <summary>
 /// 実行結果を取得します。
 /// </summary>
 /// <typeparam name="TResponse">レスポンスの型</typeparam>
 /// <typeparam name="TResult">実行結果の型</typeparam>
 /// <param name="call">呼び出しオブジェクト</param>
 /// <param name="converter">実行結果への変換処理</param>
 /// <returns>実行結果</returns>
 public static async Task <GrpcResult <TResult> > GetResultAsync <TResponse, TResult>(this AsyncUnaryCall <TResponse> call, Converter <TResponse, TResult> converter)
     where TResponse : class
 {
     try
     {
         return(GrpcResult.Create <TResult>(converter(await call.ResponseAsync.ConfigureAwait(false)), call.ToInterface()));
     }
     catch (Exception ex) when(GrpcExceptionUtility.HasRpcException(ex))
     {
         return(HandleResponseException <TResponse, TResult>(call, ex));
     }
     finally
     {
         call.Dispose();
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 全てのリクエストを送信します。
        /// </summary>
        /// <typeparam name="TRequest">リクエストの型</typeparam>
        /// <typeparam name="TResponse">レスポンスの型</typeparam>
        /// <param name="call">呼び出しオブジェクト</param>
        /// <param name="requests">リクエストを取得する処理</param>
        /// <returns>実行結果</returns>
        public static async Task <GrpcResult <TResponse> > WriteAndCompleteAsync <TRequest, TResponse>(this AsyncClientStreamingCall <TRequest, TResponse> call, IEnumerable <AsyncFunc <TRequest> > requests)
            where TRequest : class where TResponse : class
        {
            try
            {
                foreach (AsyncFunc <TRequest> request in requests)
                {
                    await call.RequestStream.WriteAsync(await request().ConfigureAwait(false)).ConfigureAwait(false);
                }

                await CompleteAsyncInternal(call).ConfigureAwait(false);

                return(GrpcResult.Create <TResponse>(await call.ResponseAsync.ConfigureAwait(false), call.ToInterface()));
            }
            catch (Exception ex) when(GrpcExceptionUtility.HasRpcException(ex))
            {
                return(HandleResponseException(call, ex));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 全てのレスポンスを受信します。
        /// </summary>
        /// <typeparam name="TResponse">レスポンスの型</typeparam>
        /// <typeparam name="TResult">実行結果の型</typeparam>
        /// <param name="call">呼び出しオブジェクト</param>
        /// <param name="converter">実行結果への変換処理</param>
        /// <returns>実行結果</returns>
        public static async Task <GrpcResult <IList <TResult> > > ReadAllAsync <TResponse, TResult>(this AsyncServerStreamingCall <TResponse> call, Converter <TResponse, TResult> converter)
            where TResponse : class
        {
            try
            {
                List <TResult> list = new List <TResult>();

                while (await call.ResponseStream.MoveNext().ConfigureAwait(false))
                {
                    list.Add(converter(call.ResponseStream.Current));
                }

                return(GrpcResult.Create <IList <TResult> >(list, call.ToInterface()));
            }
            catch (Exception ex) when(GrpcExceptionUtility.HasRpcException(ex))
            {
                return(HandleResponseListException <TResponse, TResult>(call, ex));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 全てのレスポンスを受信します。
        /// </summary>
        /// <typeparam name="TRequest">リクエストの型</typeparam>
        /// <typeparam name="TResponse">レスポンスの型</typeparam>
        /// <param name="call">呼び出しオブジェクト</param>
        /// <returns>実行結果</returns>
        public static async Task <GrpcResult <IList <TResponse> > > ReadAllAsync <TRequest, TResponse>(this AsyncDuplexStreamingCall <TRequest, TResponse> call)
            where TRequest : class where TResponse : class
        {
            try
            {
                List <TResponse> list = new List <TResponse>();

                while (await call.ResponseStream.MoveNext().ConfigureAwait(false))
                {
                    list.Add(call.ResponseStream.Current);
                }

                return(GrpcResult.Create <IList <TResponse> >(await Task.FromResult(list).ConfigureAwait(false), call.ToInterface()));
            }
            catch (Exception ex) when(GrpcExceptionUtility.HasRpcException(ex))
            {
                return(HandleResponseListException(call, ex));
            }
        }