Writing to Channel
Writing to a channel will trigger the output pipeline and ultimately write the data to the underlying socket. However, there are two distinct ways of doing this and it's important to understand the differences between them.
Buffered
All channels have an *output buffer` that is accessible through the input pipeline context. At the end of the input pipeline, if the buffer has any data, it will trigger the output pipeline and write the data to the underlying socket.
Unless you have a specific reason, you should always write to the output buffer.
Tip
For most cases, this is the recommended way of writing to the channel. The data will be buffered and only written to the underlying socket at the end of the input pipeline.
This is a synchronous process that writes the data to the pipeline buffer. When the input pipeline reaches the end, the output pipeline is triggered and the buffered data is written to the underlying socket. If the input pipeline is interrupted, either because an adapter didn't forward any data or a middleware component crashed, the data in the buffer will be discarded.
public override async Task ExecuteAsync( IAdapterContext context, IEnumerable<Message> data )
{
// ...
context.Output.Write( replyData );
}
Channel
There are be scenarios where the pipeline context might not be available, or that data has to be immediately written to the channel, no matter what happens next. In these cases, it's possible to write directly to the channel.
This is an asynchronous method that immediately triggers the output pipeline, writing the data to the underlying socket. When using this method in a middleware component, make sure this is the intended behaviour.
Note
You should use this method when there isn't a pipeline context, for example, when implementing a channel service.