1

Since IAsyncEnumerable is not available in ASP.NET Web API (.NET 4.8). Is there any alternative so that I can stream json data?

I wanted to stream an array of json data instead of sending it as a whole.

In .NET Core, I can do the following:

    async IAsyncEnumerable<String> getValues() {
        yield return "test"; 
        for(int i = 0; i < 3; i++) {
            await Task.Delay(1000); 
            yield return "Test " + i; 
        }
    }
    
    app.MapGet("/addmq/v1/inventory/databases/{database}/akastream2", () => {
        return getValues(); 
    });

And then I can use curl -N then I can see each string in the terminal every second. How should I do that in .NET Framework 4.8?

Lance
  • 2,774
  • 4
  • 37
  • 57
  • 1
    It's available through the Microsoft.Bcl.Async package. That won't allow you to send Streaming Json aka Json Lines aka NDJSON data though, even in ASP.NET Core. It's no different than returning an `IEnumerable<>` in that respect. The response is still a JSON array. What `IAsyncEnumerable` allows you to do compared to `IEnumerable<>` is produce individual items without blocking – Panagiotis Kanavos Jul 27 '22 at 15:00
  • 1
    You can return Streaming Json data if you write to the response stream directly. Using Microsoft.Bcl.Async will allow you to do so with an `await foreach` loop – Panagiotis Kanavos Jul 27 '22 at 15:05
  • Hi @PanagiotisKanavos, What I want to do is to produce the items individually without blocking. I updated my question with a .NET Core example. How can I do that in .NET Framework 4.8? I can't use asyncenumerable in my controller the route is not found when I tried to. Or am I missing something? – Lance Jul 27 '22 at 15:36
  • 1
    What does `produce the items individually` mean? Even with IAsyncEnumerable, the results will be `[{...},{...}]`. Not `{....}n{.....}`. You can load the asynchronously data any way you want, even using `await foreach` if you use `Microsoft.Bcl.Async`, but at the end you'll have to return the data as an `IEnumerable<>` or array. It's the same in ASP.NET Core. – Panagiotis Kanavos Jul 27 '22 at 15:43
  • 1
    *On the other hand* if you're OK with returning a JSON the question's code is doing, the only alternative in ASP.NET Old is to write directly to `Response`. You should keep in mind that web servers don't like long-running responses though. IIS would kill this code after a while – Panagiotis Kanavos Jul 27 '22 at 15:45
  • 1
    If you want minimal overhead, you'll have to move to ASP.NET Core. It's not just `IAsyncEnumerable` but the entire pipeline that's leaner and lighter. By orders of magnitude. This would also allow you to use real streaming protocols like gRPC. I created a streaming gRPC service recently to replicate large amounts of data (potentially 100K) between services – Panagiotis Kanavos Jul 27 '22 at 15:46
  • Maybe I should look at gRPC. If you can summarize your answer, I'll mark it as the correct answer. I think you exhausted all the possible ways to do it. Thanks a lot! – Lance Jul 27 '22 at 15:55

0 Answers0