C
C#2y ago
Kosta

Streaming a csv file to a console app with minimal api-How?

Hey Guys got this methode:
public string MakeHttpCall()
{
var watch = new Stopwatch();
Stopwatch.StartNew();
using (var reader = new StreamReader(@"C:\Users\kosta\source\repos\gRPCDemoUsingNET6\gRPCDemoUsingNET6\Data"))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');

}
return string.Empty;
}
}
}
public string MakeHttpCall()
{
var watch = new Stopwatch();
Stopwatch.StartNew();
using (var reader = new StreamReader(@"C:\Users\kosta\source\repos\gRPCDemoUsingNET6\gRPCDemoUsingNET6\Data"))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');

}
return string.Empty;
}
}
}
And this one in the Ui:
var client = new HttpClient();
await using Stream stream =
await client.GetStreamAsync("http://localhost:5276");
var lines =
await JsonSerializer.DeserializeAsync<List<string>>(stream);
foreach (var line in lines)
{
Console.WriteLine(line);

}
var client = new HttpClient();
await using Stream stream =
await client.GetStreamAsync("http://localhost:5276");
var lines =
await JsonSerializer.DeserializeAsync<List<string>>(stream);
foreach (var line in lines)
{
Console.WriteLine(line);

}
I want to stream the csv file to the ui, which happens here
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');

}
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');

}
Unsure how to do that
2 Replies
phaseshift
phaseshift2y ago
you should use IAsyncEnumerable<> in the endpoint Some reading/examples: https://anthonychu.ca/post/async-streams-dotnet-core-3-iasyncenumerable/
Async Streams with IAsyncEnumerable in .NET Core 3
One of the most exciting features of .NET Core 3 and C# 8.0 has been the addition of IAsyncEnumerable<T> (aka async streams). But what's so special about it? What can we do now that wasn't possible before?In this article, we'll look at what challenges IAsyncEnumerable<T> is intended to solve, how to implement it in our own applications, and why ...
Kosta
Kosta2y ago
@phaseshift that worked. amazing, thanks