Skip to content

Writing to Channel

Writing to a channel will trigger the output pipeline and ultimately write the data to the underlying transport. 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 transport. If the input pipeline is interrupted (because an adapter did not forward data), the buffered data is discarded. This prevents partially processed or inconsistent responses from being sent.

public override async Task ExecuteAsync( IAdapterContext context, IEnumerable<Message> data, CancellationToken cancellationToken )
{
    // ...

    context.Output.Write( replyData );
}

Unless you have a specific reason not to, you should 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 transport at the end of the input pipeline.

Channel

There may be scenarios where the pipeline context is not available, or where data must be written immediately, regardless of what happens next. In these cases, it's possible to write directly to the channel.

IChannel channel = ;// ...

await channel.WriteAsync( replyData );

This asynchronous method immediately triggers the output pipeline and writes the data to the underlying transport. 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.