Skip to content

Instantly share code, notes, and snippets.

@alistairjevans
Created June 16, 2019 09:27
Embed
What would you like to do?
Load Data in Serial
async static Task Main(string[] args)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://myserver");
using (var fileSource = new StreamReader(File.OpenRead(@"C:\Data\Sources\myfile.csv")))
{
await StreamData(fileSource, httpClient, "/api/send");
}
}
private static async Task StreamData(StreamReader fileSource, HttpClient httpClient, string path)
{
string line;
// Read from the file until it's empty
while ((line = await fileSource.ReadLineAsync()) != null)
{
// Convert a line of data into JSON compatible with the API
var jsonMsg = GetDataJson(line);
// Send it to the server
await httpClient.PostAsync(path, new StringContent(jsonMsg, Encoding.UTF8, "application/json"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment