Exemplo n.º 1
0
        private async Task ValidateAsync <TRequest>(TRequest request, IValidator <TRequest> validator)
        {
            var results = await validator.ValidateAsync(request);

            if (!results.IsValid)
            {
                var message = await _handler.HandleAsync(results.Errors);

                throw new RpcException(new Status(StatusCode.InvalidArgument, message));
            }
        }
        private async Task ValidateRequest <TRequest>(TRequest request) where TRequest : class
        {
            if (_locator.TryGetValidator <TRequest>(out var validator))
            {
                var results = await validator.ValidateAsync(request);

                if (!results.IsValid && results.Errors.Any())
                {
                    var message = await _handler.HandleAsync(results.Errors);

                    var validationMetadata = results.Errors.ToValidationMetadata();
                    throw new RpcException(new Status(StatusCode.InvalidArgument, message), validationMetadata);
                }
            }
        }
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(
            TRequest request,
            ServerCallContext context,
            UnaryServerMethod <TRequest, TResponse> continuation)
        {
            if (_locator.TryGetValidator <TRequest>(out var validator))
            {
                var results = await validator.ValidateAsync(request);

                if (!results.IsValid && results.Errors.Any())
                {
                    var message = await _handler.HandleAsync(results.Errors);

                    var validationMetadata = results.Errors.ToValidationMetadata();
                    throw new RpcException(new Status(StatusCode.InvalidArgument, message), validationMetadata);
                }
            }
            return(await continuation(request, context));
        }
Exemplo n.º 4
0
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(
            TRequest request,
            ServerCallContext context,
            UnaryServerMethod <TRequest, TResponse> continuation)
        {
            if (_provider.TryGetValidator <TRequest>(out var validator))
            {
                var results = await validator.ValidateAsync(request);

                if (!results.IsValid)
                {
                    await _handler.HandleAsync(results.Errors, context);

                    var response = Expression.Lambda <Func <TResponse> >(Expression.Block(typeof(TResponse), Expression.New(typeof(TResponse)))).Compile();
                    return(response());
                }
            }

            return(await continuation(request, context));
        }
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(
            TRequest request,
            ServerCallContext context,
            UnaryServerMethod <TRequest, TResponse> continuation)
        {
            if (_locator.TryGetValidator <TRequest>(out var validator))
            {
                var results = await validator.ValidateAsync(request);

                if (!results.IsValid)
                {
                    var message = await _handler.HandleAsync(results.Errors);

                    context.Status = new Status(StatusCode.InvalidArgument, message);
                    context.GetHttpContext().Response.Headers[GrpcProtocolConstants.StatusTrailerName] = GrpcProtocolConstants.StatusTrailerInvalidArgument;
                    return(ObjectCreator <TResponse> .Empty);
                }
            }

            return(await continuation(request, context));
        }