示例#1
0
        protected override Task <Stream> CreateContentReadStreamAsync()
        {
            // WriteMessageAsync might run synchronously and try to write to the stream. ProducerConsumerStream
            // will block on the write until the stream is being read from. The WriteMessageAsync method needs
            // to run on a different thread to prevent a deadlock.
            _stream = new ProducerConsumerStream();
            var bufferedStream = new BufferedWriteStream(_stream, WriteBufferSize);

            Task.Run(() => _messageEncoder.WriteMessageAsync(_message, bufferedStream));
            return(Task.FromResult(_stream));
        }
示例#2
0
        protected override Task <Stream> CreateContentReadStreamAsync()
        {
            // WriteMessageAsync might run synchronously and try to write to the stream. ProducerConsumerStream
            // will block on the write until the stream is being read from. The WriteMessageAsync method needs
            // to run on a different thread to prevent a deadlock.
            var resultStream = new ProducerConsumerStream();

            _stream = new BufferedWriteStream(resultStream, WriteBufferSize);
            Task.Factory.StartNew(async(content) =>
            {
                var thisPtr = content as StreamedMessageContent;
                try
                {
                    await _messageEncoder.WriteMessageAsync(thisPtr._message, thisPtr._stream);
                }
                finally
                {
                    thisPtr._stream.Dispose();
                    thisPtr._writeCompletedTcs.TrySetResult(true);
                }
            }, this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

            return(Task.FromResult <Stream>(resultStream));
        }
示例#3
0
        protected override Task<Stream> CreateContentReadStreamAsync()
        {
            // WriteMessageAsync might run synchronously and try to write to the stream. ProducerConsumerStream
            // will block on the write until the stream is being read from. The WriteMessageAsync method needs
            // to run on a different thread to prevent a deadlock.
            var resultStream = new ProducerConsumerStream();
            _stream = new BufferedWriteStream(resultStream, WriteBufferSize);
            Task.Factory.StartNew(async (content) =>
            {
                var thisPtr = content as StreamedMessageContent;
                try
                {
                    await _messageEncoder.WriteMessageAsync(thisPtr._message, thisPtr._stream);
                }
                finally
                {
                    thisPtr._stream.Dispose();
                    thisPtr._writeCompletedTcs.TrySetResult(true);
                }
            }, this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

            return Task.FromResult<Stream>(resultStream);
        }
 protected override Task<Stream> CreateContentReadStreamAsync()
 {
     // WriteMessageAsync might run synchronously and try to write to the stream. ProducerConsumerStream
     // will block on the write until the stream is being read from. The WriteMessageAsync method needs
     // to run on a different thread to prevent a deadlock.
     _stream = new ProducerConsumerStream();
     var bufferedStream = new BufferedWriteStream(_stream, WriteBufferSize);
     Task.Run(async () => await _messageEncoder.WriteMessageAsync(_message, bufferedStream));
     return Task.FromResult(_stream);
 }