Last active
March 23, 2021 14:25
-
-
Save ry8806/b9da1198784feab60a6e22920d486600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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