Exemple #1
0
        /// <summary>
        /// サービスメソッドが呼び出されたときの処理を行います。
        /// </summary>
        /// <param name="context">コンテキスト</param>
        /// <param name="interceptors">割込処理</param>
        /// <param name="performanceListener">パフォーマンスリスナー</param>
        /// <returns></returns>
        private async Task OnExecutedServiceMethodAsync(ServerCallContext context, IEnumerable <IGrpcServerMethodInvokedInterceptor> interceptors, GrpcServerPerformanceListener performanceListener)
        {
            if (interceptors != null)
            {
                foreach (IGrpcServerMethodInvokedInterceptor interceptor in interceptors)
                {
                    if (interceptor == null)
                    {
                        continue;
                    }

                    Stopwatch watch = Stopwatch.StartNew();

                    try
                    {
                        await interceptor.OnInvokedAsync(context).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        throw new GrpcServerMethodException(string.Format(Properties.MessageResources.ServerMethodInterceptorFailed + ex.Message, context.Method, interceptor.Name), ex, context, interceptor);
                    }
                    finally
                    {
                        if (performanceListener != null)
                        {
                            performanceListener.NotifyMethodIntercepted(context, interceptor, GrpcPerformanceListener.GetMilliseconds(watch));
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 例外を処理します。
        /// </summary>
        /// <param name="context">コンテキスト</param>
        /// <param name="exceptionHandlers">例外ハンドラ</param>
        /// <param name="performanceListener">パフォーマンスリスナー</param>
        /// <param name="original">発生した例外</param>
        /// <param name="alternate">代わりにスローする例外</param>
        /// <returns>例外がラップされた場合、true を返します。</returns>
        private bool HandleException(ServerCallContext context, IEnumerable <IGrpcServerMethodExceptionHandler> exceptionHandlers, GrpcServerPerformanceListener performanceListener, Exception original, out Exception alternate)
        {
            Exception alt = null;

            if (exceptionHandlers != null)
            {
                foreach (IGrpcServerMethodExceptionHandler handler in exceptionHandlers)
                {
                    if (handler == null)
                    {
                        continue;
                    }

                    Stopwatch watch = Stopwatch.StartNew();

                    try
                    {
                        if (handler.RelpaceException(context, original, out alt))
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new GrpcServerMethodException(string.Format(Properties.MessageResources.ServerMethodInterceptorFailed + ex.Message, context.Method, handler.Name), ex, context, handler);
                    }
                    finally
                    {
                        if (performanceListener != null)
                        {
                            performanceListener.NotifyMethodIntercepted(context, handler, GrpcPerformanceListener.GetMilliseconds(watch));
                        }
                    }
                }
            }

            if (alt == null)
            {
                alt = original;
            }

            RpcException rpc = alt as RpcException;

            if (rpc == null)
            {
                alternate = CreateRpcException(context, alt);
            }
            else
            {
                alternate = rpc;
            }

            return(!object.Equals(original, alternate));
        }