Return IAsyncEnumerable<T> how to set the MaxIAsyncEnumerableBufferLimit on controller level

Viewed 721

I am implementing buffering on .net core 3.1, so i am returning

public async IAsyncEnumerable<Product> GetAllProducts()
{

} 

I am setting the MaxIAsyncEnumerableBufferLimit on .AddControllers(options => options.MaxIAsyncEnumerableBufferLimit = 'N')

So this will be applied to all controllers. Is there anyway to set MaxIAsyncEnumerableBufferLimit on individual controller level, because I want to implement buffering only on select few controllers

1 Answers

The answer to your question is unfortunately no, you cannot set it at the controller-level.

A search of the aspnet.core github for IAsyncEnumerableBufferLimit brings us to the AsyncEnumerableReader class. You can see on line 88 that the reader only looks at the single property on the MvcOptions:

if (count++ >= _mvcOptions.MaxIAsyncEnumerableBufferLimit)
{
      throw new InvalidOperationException(Resources.FormatObjectResultExecutor_MaxEnumerationExceeded(
                        nameof(AsyncEnumerableReader),
                        value.GetType()));
}

The ObjectResultExecutor class uses the AsyncEnumerableReaderclass under the hood when it detects an async enumerable. It passes no other options than the default MvcOptions when it constructs the instance:

_asyncEnumerableReaderFactory = new AsyncEnumerableReader(options);
Related