Пример #1
0
        async Task <byte[]> ServerStreamingServerMethod <TRequest, TResponse>(byte[] request, IServerStreamWriter <byte[]> responseStream, ServerCallContext context)
        {
            using var serviceLocatorScope = serviceProvider.CreateScope();

            var isErrorOrInterrupted = false;
            var serviceContext       = new ServiceContext(ServiceType, MethodInfo, AttributeLookup, this.MethodType, context, serializerOptions, logger, this, serviceLocatorScope.ServiceProvider)
            {
                ResponseStream = responseStream,
            };

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

                return(emptyBytes);
            }
            catch (ReturnStatusException ex)
            {
                isErrorOrInterrupted = true;
                context.Status       = ex.ToStatus();
                return(emptyBytes);
            }
            catch (Exception ex)
            {
                isErrorOrInterrupted = true;
                if (isReturnExceptionStackTraceInErrorDetail)
                {
                    context.Status = new Status(StatusCode.Unknown, ex.ToString());
                    logger.Error(ex, context);
                    return(emptyBytes);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                logger.EndInvokeMethod(serviceContext, emptyBytes, typeof(Nil), (DateTime.UtcNow - serviceContext.Timestamp).TotalMilliseconds, isErrorOrInterrupted);
            }
        }
Пример #2
0
        async Task <byte[]> UnaryServerMethod <TRequest, TResponse>(byte[] request, ServerCallContext context)
        {
            using var serviceLocatorScope = serviceProvider.CreateScope();

            var isErrorOrInterrupted = false;
            var serviceContext       = new ServiceContext(ServiceType, MethodInfo, AttributeLookup, this.MethodType, context, serializerOptions, logger, this, serviceLocatorScope.ServiceProvider);

            serviceContext.SetRawRequest(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);
                    logger.Error(ex, context);
                    response = emptyBytes;
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                logger.EndInvokeMethod(serviceContext, response, typeof(TResponse), (DateTime.UtcNow - serviceContext.Timestamp).TotalMilliseconds, isErrorOrInterrupted);
            }

            return(response);
        }