private Task OnDispatch()

in src/Microsoft.ServiceFabric.Services.Remoting/V1/Runtime/ServiceRemotingDispatcher.cs [180:260]


        private Task<byte[]> OnDispatch(
                            ServiceRemotingMessageHeaders headers,
                            byte[] requestBodyBytes,
                            CancellationToken cancellationToken)
        {
            if (!this.methodDispatcherMap.TryGetValue(headers.InterfaceId, out var methodDispatcher))
            {
                throw new NotImplementedException(string.Format(
                    CultureInfo.CurrentCulture,
                    SR.ErrorInterfaceNotImplemented,
                    headers.InterfaceId,
                    this.service));
            }

            Task<object> dispatchTask = null;
            var stopwatch = Stopwatch.StartNew();
            var requestBody = methodDispatcher.DeserializeRequestMessageBody(requestBodyBytes);

            if (this.servicePerformanceCounterProvider.ServiceRequestDeserializationTimeCounterWriter != null)
            {
                this.servicePerformanceCounterProvider.ServiceRequestDeserializationTimeCounterWriter.UpdateCounterValue(
                    stopwatch.ElapsedMilliseconds);
            }

            stopwatch.Restart();
            try
            {
                dispatchTask = methodDispatcher.DispatchAsync(
                    this.service,
                    headers.MethodId,
                    requestBody,
                    cancellationToken);
            }
            catch (Exception e)
            {
                var info = ExceptionDispatchInfo.Capture(e);
                this.servicePerformanceCounterProvider.OnServiceMethodFinish(
                    headers.InterfaceId,
                    headers.MethodId,
                    stopwatch.Elapsed,
                    e);
                info.Throw();
            }

            return dispatchTask.ContinueWith(
                t =>
                {
                    object responseBody = null;
                    try
                    {
                        responseBody = t.GetAwaiter().GetResult();
                    }
                    catch (Exception e)
                    {
                        var info = ExceptionDispatchInfo.Capture(e);

                        this.servicePerformanceCounterProvider.OnServiceMethodFinish(
                            headers.InterfaceId,
                            headers.MethodId,
                            stopwatch.Elapsed,
                            e);
                        info.Throw();
                    }

                    this.servicePerformanceCounterProvider.OnServiceMethodFinish(
                        headers.InterfaceId,
                        headers.MethodId,
                        stopwatch.Elapsed);

                    stopwatch.Restart();
                    var response = methodDispatcher.SerializeResponseMessageBody(responseBody);
                    if (this.servicePerformanceCounterProvider.ServiceResponseSerializationTimeCounterWriter != null)
                    {
                        this.servicePerformanceCounterProvider.ServiceResponseSerializationTimeCounterWriter
                            .UpdateCounterValue(stopwatch.ElapsedMilliseconds);
                    }

                    return response;
                },
                TaskContinuationOptions.ExecuteSynchronously);
        }