Example #1
0
        public async Task <ServiceCallResult> DispatchAsync(TService service, RpcMessage message)
        {
            if (!AsyncOperationCodesMap.TryGetValue(message.OperationCode, out var operation))
            {
                _logger.LogError(
                    $"Method with operation code {message.OperationCode} was not found in service with code {message.ServiceCode}");
                return(ServiceCallResult.CreateServiceCallResultWithException(
                           ExceptionsSerializer.Instance.Serialize(
                               new RpcDispatchingException(message.ServiceCode, message.OperationCode))));
            }

            try
            {
                var asyncOperationCallResult = operation(service, message.ArgumentsData);

                if (asyncOperationCallResult.IsVoid)
                {
                    await asyncOperationCallResult.Task;
                    return(ServiceCallResult.GetVoidServiceCallResult());
                }

                await asyncOperationCallResult.Task.ConfigureAwait(false);

                return(asyncOperationCallResult.GetResult(asyncOperationCallResult.Task));
            }
            catch (Exception exception)
            {
                return(ServiceCallResult.CreateServiceCallResultWithException(
                           ExceptionsSerializer.Instance.Serialize(exception)));
            }
        }
        public ServiceCallResult Dispatch(TService serviceInstance, RpcMessage message)
        {
            if (!OperationCodesMap.TryGetValue(message.OperationCode, out var operation))
            {
                _logger.LogError(
                    $"Method with operation code {message.OperationCode} was not found in service with code {message.ServiceCode}");
                return(ServiceCallResult.CreateServiceCallResultWithException(
                           ExceptionsSerializer.Instance.Serialize(
                               new RpcDispatchingException(message.ServiceCode, message.OperationCode))));
            }

            try
            {
                return(operation(serviceInstance, message.ArgumentsData));
            }
            catch (Exception exception)
            {
                return(ServiceCallResult.CreateServiceCallResultWithException(
                           ExceptionsSerializer.Instance.Serialize(exception)));
            }
        }