Skip to content

Instantly share code, notes, and snippets.

@alistairjevans
Created June 16, 2019 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistairjevans/bddd44f5c59845cb8c87042e5dcbc5b1 to your computer and use it in GitHub Desktop.
Save alistairjevans/bddd44f5c59845cb8c87042e5dcbc5b1 to your computer and use it in GitHub Desktop.
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