Exemple #1
0
        async Task <byte[]> UnaryServerMethod <TRequest, TResponse>(byte[] request, ServerCallContext context)
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();
            var isErrorOrInterrupted = false;
            var serviceContext       = new ServiceContext(ServiceType, MethodInfo, AttributeLookup, this.MethodType, context, resolver, logger)
            {
                Request = request
            };

            byte[] response = emptyBytes;
            try
            {
                logger.BeginInvokeMethod(serviceContext, request);
                await this.methodBody(serviceContext).ConfigureAwait(false);

                response = serviceContext.Result ?? emptyBytes;
            }
            catch (ReturnStatusException ex)
            {
                isErrorOrInterrupted = true;
                context.Status       = ex.ToStatus();
                response             = emptyBytes;
            }
            catch (Exception ex)
            {
                isErrorOrInterrupted = true;
                if (isReturnExceptionStackTraceInErrorDetail)
                {
                    context.Status = new Status(StatusCode.Unknown, ex.ToString());
                    LogError(ex, context);
                    response = emptyBytes;
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                sw.Stop();
                logger.EndInvokeMethod(serviceContext, response, sw.Elapsed.TotalMilliseconds, isErrorOrInterrupted);
            }

            return(response);
        }
        async Task <byte[]> UnaryServerMethod <TRequest, TResponse>(byte[] request, ServerCallContext context)
        {
            var isErrorOrInterrupted = false;
            var serviceContext       = new ServiceContext(ServiceType, MethodInfo, AttributeLookup, this.MethodType, context, resolver, logger)
            {
                Request = request
            };

            byte[] response = emptyBytes;
            try
            {
                logger.BeginInvokeMethod(serviceContext, request, typeof(TRequest));
                if (enableCurrentContext)
                {
                    ServiceContext.currentServiceContext.Value = serviceContext;
                }
                await this.methodBody(serviceContext).ConfigureAwait(false);

                response = serviceContext.Result ?? emptyBytes;
            }
            catch (ReturnStatusException ex)
            {
                isErrorOrInterrupted = true;
                context.Status       = ex.ToStatus();
                response             = emptyBytes;
            }
            catch (Exception ex)
            {
                isErrorOrInterrupted = true;
                if (isReturnExceptionStackTraceInErrorDetail)
                {
                    // Trim data.
                    var msg       = ex.ToString();
                    var lineSplit = msg.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                    var sb        = new System.Text.StringBuilder();
                    for (int i = 0; i < lineSplit.Length; i++)
                    {
                        if (!(lineSplit[i].Contains("System.Runtime.CompilerServices") ||
                              lineSplit[i].Contains("直前に例外がスローされた場所からのスタック トレースの終わり") ||
                              lineSplit[i].Contains("End of stack trace from the previous location where the exception was thrown")
                              ))
                        {
                            sb.AppendLine(lineSplit[i]);
                        }
                        if (sb.Length >= 5000)
                        {
                            sb.AppendLine("----Omit Message(message size is too long)----");
                            break;
                        }
                    }
                    var str = sb.ToString();

                    context.Status = new Status(StatusCode.Unknown, str);
                    LogError(ex, context);
                    response = emptyBytes;
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                logger.EndInvokeMethod(serviceContext, response, typeof(TResponse), (DateTime.UtcNow - serviceContext.Timestamp).TotalMilliseconds, isErrorOrInterrupted);
            }

            return(response);
        }