예제 #1
0
        public ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
        {
            var result = new DivideByResult {
                IsSuccess = true
            };

            // invoke ValidateParameterAttribute.Validate for marked method parameters
            var parameters = context.ServiceMethodInfo.GetParameters();

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];
                foreach (var attribute in parameter.GetCustomAttributes <ValidateParameterAttribute>())
                {
                    attribute.Validate(context.Request[i], parameter.Name, result);
                }
            }

            if (!result.IsSuccess)
            {
                // skip the method: pass non-success result to the client
                context.Response[0] = result;
                return(new ValueTask(Task.CompletedTask));
            }

            // call the method
            return(next());
        }
예제 #2
0
            public async ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
            {
                var clientStream = (IAsyncEnumerable <int>?)context.Request.Stream;

                if (clientStream != null)
                {
                    await foreach (var i in clientStream.ConfigureAwait(false))
                    {
                    }
                }
            }
예제 #3
0
        public override ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
        {
            var x = (int)context.Request["x"] !;
            var y = (int)context.Request["y"] !;

            // do not call Calculator.SumAsync
            // await next().ConfigureAwait(false);

            context.Response["result"] = x + y;

            return(new ValueTask(Task.CompletedTask));
        }
예제 #4
0
        public override async ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
        {
            var inputMultiplier = (int)context.Request["multiplier"] !;
            var inputValues     = (IAsyncEnumerable <int>)context.Request.Stream;

            // increase multiplier by 2
            context.Request["multiplier"] = inputMultiplier + 2;

            // increase each input value by 1
            context.Request.Stream = IncreaseValuesBy1(inputValues, context.ServerCallContext.CancellationToken);

            await next().ConfigureAwait(false);

            var outputValues = (IAsyncEnumerable <int>)context.Response.Stream;

            // increase each output value by 1
            context.Response.Stream = IncreaseValuesBy1(outputValues, context.ServerCallContext.CancellationToken);
        }
예제 #5
0
        public async ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
        {
            // create logger with a service name
            var logger = _loggerFactory.CreateLogger(context.ServiceInstance.GetType().Name);

            // log input
            LogBegin(logger, context.ContractMethodInfo.Name, context.Request);

            // log client stream in case of Client/Duplex streaming
            if (context.Request.Stream != null)
            {
                context.Request.Stream = LogWrapStream(
                    logger,
                    context.ContractMethodInfo.Name + " client stream",
                    context.Request.Stream,
                    context.ServerCallContext.CancellationToken);
            }

            try
            {
                // invoke all other filters in the stack and the service method
                await next().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                // log exception
                logger.LogError("error {0} failed: {1}", context.ContractMethodInfo.Name, ex);
                throw;
            }

            // log server stream in case of Server/Duplex streaming
            if (context.Response.Stream != null)
            {
                context.Response.Stream = LogWrapStream(
                    logger,
                    context.ContractMethodInfo.Name + " server stream",
                    context.Response.Stream,
                    context.ServerCallContext.CancellationToken);
            }

            // log output
            LogEnd(logger, context.ContractMethodInfo.Name, context.Response);
        }
예제 #6
0
        public async ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
        {
            context.ServiceMethodInfo.ShouldNotBeNull();

            var input = (IList <string>)context.Request[0] !;

            context.Request["input"] = new List <string>(input)
            {
                Name + "-before"
            };

            await next().ConfigureAwait(false);

            var result = (IList <string>)context.Response[0] !;

            context.Response[0] = new List <string>(result)
            {
                Name + "-after"
            };
        }
예제 #7
0
 public override ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next)
 {
     throw new NotImplementedException();
 }
예제 #8
0
 /// <inheritdoc />
 public abstract ValueTask InvokeAsync(IServerFilterContext context, Func <ValueTask> next);