Esempio n. 1
0
        public async Task WriteAsync_WriteInProgress_Error()
        {
            // Arrange
            var tcs = new TaskCompletionSource <object?>(TaskCreationOptions.RunContinuationsAsynchronously);

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(new TestResponseBodyFeature(PipeWriter.Create(new MemoryStream()), startAsyncTask: tcs.Task));
            var serverCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(httpContext);
            var writer            = new HttpContextStreamWriter <HelloReply>(serverCallContext, MessageHelpers.ServiceMethod.ResponseMarshaller.ContextualSerializer);

            // Act
            _ = writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 1"
            });

            var ex = await ExceptionAssert.ThrowsAsync <InvalidOperationException>(() =>
            {
                return(writer.WriteAsync(new HelloReply
                {
                    Message = "Hello world 2"
                }));
            });

            // Assert
            Assert.AreEqual("Can't write the message because the previous write is in progress.", ex.Message);
        }
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
        {
            // Decode request
            var request = await httpContext.Request.BodyReader.ReadSingleMessageAsync <TRequest>(serverCallContext, MethodInvoker.Method.RequestMarshaller.ContextualDeserializer);

            var streamWriter = new HttpContextStreamWriter <TResponse>(serverCallContext, MethodInvoker.Method.ResponseMarshaller.ContextualSerializer);
            await _invoker.Invoke(httpContext, serverCallContext, request, streamWriter);
        }
        protected override Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
        {
            // Disable request body data rate for client streaming
            DisableMinRequestBodyDataRateAndMaxRequestBodySize(httpContext);

            var streamReader = new HttpContextStreamReader <TRequest>(serverCallContext, MethodInvoker.Method.RequestMarshaller.ContextualDeserializer);
            var streamWriter = new HttpContextStreamWriter <TResponse>(serverCallContext, MethodInvoker.Method.ResponseMarshaller.ContextualSerializer);

            return(_invoker.Invoke(httpContext, serverCallContext, streamReader, streamWriter));
        }
Esempio n. 4
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpApiServerCallContext serverCallContext)
        {
            // Decode request
            var request = await JsonRequestHelpers.ReadMessage <TRequest>(serverCallContext, SerializerOptions);

            var streamWriter = new HttpContextStreamWriter <TResponse>(serverCallContext, SerializerOptions);

            try
            {
                await _invoker.Invoke(httpContext, serverCallContext, request, streamWriter);
            }
            finally
            {
                streamWriter.Complete();
            }
        }
Esempio n. 5
0
        public async Task WriteAsync_BufferHintWriteOptions_DoesNotFlush()
        {
            // Arrange
            var ms = new MemoryStream();

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IHttpResponseBodyFeature>(new TestResponseBodyFeature(PipeWriter.Create(ms)));
            var serverCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(httpContext);
            var writer            = new HttpContextStreamWriter <HelloReply>(serverCallContext, MessageHelpers.ServiceMethod.ResponseMarshaller.ContextualSerializer);

            serverCallContext.WriteOptions = new WriteOptions(WriteFlags.BufferHint);

            // Act 1
            await writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 1"
            });

            // Assert 1
            Assert.AreEqual(0, ms.Length);

            // Act 2
            await writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 2"
            });

            // Assert 2
            Assert.AreEqual(0, ms.Length);

            await httpContext.Response.BodyWriter.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);
            var pipeReader = PipeReader.Create(ms);

            var writtenMessage1 = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader);

            Assert.AreEqual("Hello world 1", writtenMessage1 !.Message);
            var writtenMessage2 = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader);

            Assert.AreEqual("Hello world 2", writtenMessage2 !.Message);
        }
Esempio n. 6
0
        public async Task WriteAsync_BufferHintWriteOptions_DoesNotFlush()
        {
            // Arrange
            var ms = new MemoryStream();

            var httpContext = new DefaultHttpContext();

            httpContext.Response.BodyPipe = new StreamPipeWriter(ms);
            var serverCallContext = new HttpContextServerCallContext(httpContext, NullLogger.Instance);
            var writer            = new HttpContextStreamWriter <HelloReply>(serverCallContext, TestServiceOptions, (message) => message.ToByteArray());

            serverCallContext.WriteOptions = new WriteOptions(WriteFlags.BufferHint);

            // Act 1
            await writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 1"
            });

            // Assert 1
            Assert.AreEqual(0, ms.Length);

            // Act 2
            await writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 2"
            });

            // Assert 2
            Assert.AreEqual(0, ms.Length);

            await httpContext.Response.BodyPipe.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);
            var pipeReader = new StreamPipeReader(ms);

            var writtenMessage1 = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader);

            Assert.AreEqual("Hello world 1", writtenMessage1.Message);
            var writtenMessage2 = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader);

            Assert.AreEqual("Hello world 2", writtenMessage2.Message);
        }
        public async Task WriteAsync_DefaultWriteOptions_Flushes()
        {
            // Arrange
            var ms = new MemoryStream();

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IResponseBodyPipeFeature>(new TestResponseBodyPipeFeature(PipeWriter.Create(ms)));
            var serverCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(httpContext);
            var writer            = new HttpContextStreamWriter <HelloReply>(serverCallContext, (message) => message.ToByteArray());

            // Act 1
            await writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 1"
            });

            // Assert 1
            Assert.AreEqual(20, ms.Length);

            // Act 2
            await writer.WriteAsync(new HelloReply
            {
                Message = "Hello world 2"
            });

            // Assert 2
            Assert.AreEqual(40, ms.Length);

            ms.Seek(0, SeekOrigin.Begin);
            var pipeReader = PipeReader.Create(ms);

            var writtenMessage1 = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader);

            Assert.AreEqual("Hello world 1", writtenMessage1 !.Message);
            var writtenMessage2 = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader);

            Assert.AreEqual("Hello world 2", writtenMessage2 !.Message);
        }