Exemple #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)));
            }
        }
Exemple #2
0
            public async Task Handle()
            {
                try
                {
                    var stream  = GetNetworkStream();
                    var message = await stream.ReadMessageAsync();

                    while (message.Any() && !message.IsEndOfSessionMessage())
                    {
                        var deserializedMessage             = _messageSerializer.Deserialize(message);
                        ServiceCallResult serviceCallResult = deserializedMessage.IsAsyncOperation
                                                        ? await _serviceDispatcher.DispatchAsync(_serviceInstance, deserializedMessage)
                                                        : _serviceDispatcher.Dispatch(_serviceInstance, deserializedMessage);

                        if (serviceCallResult.HasReturnValue || serviceCallResult.HasException)
                        {
                            var returnData = _serviceCallResultSerializer.Serialize(serviceCallResult);
                            _logger.LogDebug($"Sending {returnData.Length} bytes to caller");
                            await stream.WriteMessageAsync(returnData);
                        }

                        message = await stream.ReadMessageAsync();
                    }

                    _logger.LogDebug("EndOfSession message received. Closing client.");
                    stream.Close();
                }
                catch (Exception exception)
                {
                    _logger.LogError(exception, "Error working with channel");
                    throw;
                }
            }
        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)));
            }
        }