Skip to content

Instantly share code, notes, and snippets.

@ry8806
Last active March 23, 2021 14:25
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 ry8806/b9da1198784feab60a6e22920d486600 to your computer and use it in GitHub Desktop.
Save ry8806/b9da1198784feab60a6e22920d486600 to your computer and use it in GitHub Desktop.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// Setup the DataFlow
// CSV File Setup (do/check this one time at application start)
if (!File.Exists("entries.csv"))
{
// If it doesn't exist, write the header line
File.AppendAllLines("entries.csv", new[] { $"Email,Answer,IPAddress,Created(UTC)" });
}
// The Consumer Block - this is the block that receives an entry and write to the file
var fileWriter = new ActionBlock<CompetitionEntry>((entry) =>
{
// When the object is received, put the new csv line together
string newCsvLine = $"{entry.Email},{entry.Answer},{entry.IPAddress},{entry.Created:s}";
File.AppendAllLines("entries.csv", new[] { newCsvLine });
});
// A simple buffer (one at a time), link this to the consumer above to create a "pipeline"
var producer = new BufferBlock<CompetitionEntry>();
producer.LinkTo(fileWriter);
// Allow the controller to have the buffer injected
services.AddSingleton(producer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment