public async Task Run() { var collection = new RecipientsCollection(); collection.Add <Foo>(); collection.Add <Bar>(); collection.Add <Baz>(); var aggregator = new Aggregator(collection); // The consumer can limit the duration of the aggregation by // providing a timeout (or CancellationToken) to the aggregator: // this will ensure that the response will be ready in // the given amount of time and will "discard" incomplete // (and also never-ending) invocations. var response = await aggregator.Send(42, TimeSpan.FromMilliseconds(50)); // The recipients that didn't complete in time will be // listed in the Incomplete property of the result: Console.WriteLine($"Completed {response.Completed.Count}"); Console.WriteLine( $"Incomplete {response.Incomplete.Count}: " + $"{response.Incomplete[0].Recipient.Type?.Name}, " + $"{response.Incomplete[1].Recipient.Type?.Name}"); }
public async Task Run() { // Recipients are registered as always. var collection = new RecipientsCollection(); collection.Add <Foo>(); collection.Add <Bar>(); collection.Add <Baz>(); var aggregator = new Aggregator(collection); // The Send<TRequest> method checks only the input // parameter of the methods: AggregatedResponse <object?> all = await aggregator.Send(42); var allResults = all.AsResultsList(); // "42", 42L, 42 Console.WriteLine($"" + $"{allResults[0]} ({allResults[0]?.GetType().Name}), " + $"{allResults[1]} ({allResults[1]?.GetType().Name}), " + $"{allResults[2]} ({allResults[2]?.GetType().Name})"); // Instead the Send<TRequest, TResponse> method checks // also the return type of the methods, allowing to filter // on them and getting typed results: AggregatedResponse <string> onlyStrings = await aggregator.Send <string>(42); var onlyStringsResults = onlyStrings.AsResultsList(); // "42" Console.WriteLine($"{onlyStringsResults[0]} ({allResults[0]?.GetType().Name})"); }
public async Task Run() { var collection = new RecipientsCollection(); // Register typeless recipients using delegates. collection.Add((int n) => n.ToString()); collection.Add((int n) => n * 2); collection.Add((DateTime d) => d.Ticks); var aggregator = new Aggregator(collection); var responseOfInt = await aggregator.Send(42); var resultsOfInt = responseOfInt.AsResultsList(); // "42", 84 Console.WriteLine($"{resultsOfInt[0]}, {resultsOfInt[1]}"); var onlyStrings = await aggregator.Send <string>(42); var onlyStringResults = onlyStrings.AsResultsList(); // "42" Console.WriteLine($"{onlyStringResults[0]}"); var responseOfDateTime = await aggregator.Send(DateTime.UtcNow); var resultsOfDateTime = responseOfDateTime.AsResultsList(); // 'now.Ticks'L Console.WriteLine($"{resultsOfDateTime[0]}"); }
public async Task Run() { // Register the available recipients. var collection = new RecipientsCollection(); collection.Add <Foo>(); collection.Add <Bar>(); // Send a request and aggregate all the results. var aggregator = new Aggregator(collection); var response = await aggregator.Send(42); var results = response.AsResultsList(); // 1764L, 84 Console.WriteLine($"{results[0]}, {results[1]}"); }
public async Task Run() { var collection = new RecipientsCollection(); collection.Add <Foo>(); collection.Add <Bar>(); var aggregator = new Aggregator(collection); var response = await aggregator.Send("Don't Panic"); // The aggregated response separates the invocations // that completed successfully with a response and // the ones that failed with an exception. Console.WriteLine($"Completed {response.Completed.Count}"); Console.WriteLine( $"Faulted {response.Faulted.Count}: " + $"{response.Faulted[0].Recipient.Type?.Name} => " + $"{response.Faulted[0].Exception?.Message}"); }
public async Task Run() { var collection = new RecipientsCollection(); collection.Add <Foo>(); collection.Add <Bar>(); collection.Add <Baz>(); var aggregator = new Aggregator(collection); // It doesn't matter if a method returns // synchronously or asynchronously: // the aggregator awaits any async method // before aggregating the response. var response1 = await aggregator.Send(42); var results = response1.AsResultsList(); // 84L, 42L, "42" Console.WriteLine($"" + $"{results[0]} ({results[0]?.GetType().Name}), " + $"{results[1]} ({results[1]?.GetType().Name}), " + $"{results[2]} ({results[2]?.GetType().Name})"); // The aggregator provides a "all methods are async" abstraction // so that when using the Send<TRequest, TResponse> method // all the recipients that return either TResponse, Task<TResponse> // or ValueTask<TResponse> get invoked. var response2 = await aggregator.Send <long>(42); var longResults = response2.AsResultsList(); Console.WriteLine($"{longResults[0]} ({longResults[0].GetType().Name})"); Console.WriteLine($"{longResults[1]} ({longResults[1].GetType().Name})"); var response3 = await aggregator.Send <string>(42); var stringResults = response3.AsResultsList(); Console.WriteLine($"{stringResults[0]} ({stringResults[0].GetType().Name})"); }
static RecipientsCollection Load(Action onInvalidation) { const string configPath = "~/Elmah.ErrorMail.config"; var vpp = HostingEnvironment.VirtualPathProvider; var recipients = new RecipientsCollection(); if (vpp.FileExists(configPath)) { var entries = from line in vpp.GetFile(configPath).ReadLines() select line.Trim() into line where line.Length > 0 && line[0] != '#' select Regex.Match(line, @"^(?:(to|cc|bcc):)?(.+)") into m where m.Success select m.Groups into gs let id = gs[1].Success ? gs[1].Value[0] : 't' select new { Collection = id == 't' ? recipients.To : id == 'c' ? recipients.Cc : recipients.Bcc, Addresses = gs[2].Value, }; foreach (var e in entries) { e.Collection.Add(e.Addresses); } } HttpRuntime.Cache.Insert(CacheKey, CacheKey, vpp.GetCacheDependency(configPath, new[] { configPath }, DateTime.Now), delegate { onInvalidation(); }); return(recipients); }
public async void SendEmails() { MailPreferencesCollection myPrefs = new MailPreferencesCollection(); myPrefs.LoadAll(); if (!myPrefs.HasData) { this.MessageBoxService.ShowMessage("You must setup your email first!", "Mail Not Setup", MessageButton.OK, MessageIcon.Stop); return; } this.SplashScreenService.ShowSplashScreen(); this.SplashScreenService.SetSplashScreenState("Refreshing next showings..."); string body = ""; DataTable shows = new DataTable(); shows.Columns.Add("Name", typeof(string)); shows.Columns.Add("Time", typeof(DateTime)); shows.Columns.Add("Summary", typeof(string)); DataTable shows2 = new DataTable(); shows2.Columns.Add("Name", typeof(string)); shows2.Columns.Add("Time", typeof(DateTime)); shows2.Columns.Add("Summary", typeof(string)); foreach (TVShows show in TVShowsCollection) { var series = await TVMaze.TVMaze.GetSeries(Convert.ToUInt32(show.MazeId), true, false); this.SplashScreenService.SetSplashScreenState("Looking up show times for " + series.name + "..."); foreach (var episode in series.Episodes) { if (Convert.ToDateTime(episode.airdate).Date == DateTime.Today.Date) { var convertedDate = DateTime.SpecifyKind( DateTime.Parse(episode.airstamp.ToString()), DateTimeKind.Utc); DataRow dr = shows.NewRow(); if (series.network != null && series.network.name != "") { dr[0] = series.name + "(" + series.network.name + ")"; } else if (series.webChannel != null && series.webChannel.name != "") { dr[0] = series.name + "(" + series.webChannel.name + ")"; } else { dr[0] = series.name; } dr[1] = convertedDate; if (!string.IsNullOrEmpty(episode.summary)) { dr[2] = episode.summary; } else { dr[2] = episode.name; } shows.Rows.Add(dr); } else if (Convert.ToDateTime(episode.airdate).Date == DateTime.Today.Date.AddDays(1)) { var convertedDate = DateTime.SpecifyKind( DateTime.Parse(episode.airstamp.ToString()), DateTimeKind.Utc); DataRow dr = shows2.NewRow(); if (series.network != null && series.network.name != "") { dr[0] = series.name + "(" + series.network.name + ")"; } else if (series.webChannel != null && series.webChannel.name != "") { dr[0] = series.name + "(" + series.webChannel.name + ")"; } else { dr[0] = series.name; } dr[1] = convertedDate; if (!string.IsNullOrEmpty(episode.summary)) { dr[2] = episode.summary; } else { dr[2] = episode.name; } shows2.Rows.Add(dr); } } } if (shows.Rows.Count > 0) { var orderedRows = from row in shows.AsEnumerable() orderby row.Field <DateTime>("Time") select row; DataTable tblOrdered = orderedRows.CopyToDataTable(); body = "The following shows are scheduled to air today: " + Environment.NewLine + Environment.NewLine; foreach (var row in tblOrdered.Rows) { body += Convert.ToDateTime(((System.Data.DataRow)row).ItemArray[1]).ToShortTimeString() + ": " + ((System.Data.DataRow)row).ItemArray[0] + Environment.NewLine + ((System.Data.DataRow)row).ItemArray[2] + Environment.NewLine + Environment.NewLine; } } if (shows2.Rows.Count > 0) { var orderedRows2 = from row in shows2.AsEnumerable() orderby row.Field <DateTime>("Time") select row; DataTable tblOrdered2 = orderedRows2.CopyToDataTable(); body += "The following shows are scheduled to air tomorrow: " + Environment.NewLine + Environment.NewLine; foreach (var row in tblOrdered2.Rows) { body += Convert.ToDateTime(((System.Data.DataRow)row).ItemArray[1]).ToShortTimeString() + ": " + ((System.Data.DataRow)row).ItemArray[0] + Environment.NewLine + ((System.Data.DataRow)row).ItemArray[2] + Environment.NewLine + Environment.NewLine; } } if (shows.Rows.Count > 0 || shows2.Rows.Count > 0) { this.SplashScreenService.SetSplashScreenState("Creating and sending email..."); //SmtpClient client = new SmtpClient(); //client.Port = 587; //client.Host = "smtp.gmail.com"; //client.EnableSsl = true; //client.Timeout = 10000; //client.DeliveryMethod = SmtpDeliveryMethod.Network; //client.UseDefaultCredentials = false; //client.Credentials = new System.Net.NetworkCredential(myPrefs[0].EmailAddress, myPrefs[0].Password); RecipientsCollection myCol = new RecipientsCollection(); myCol.LoadAll(); foreach (Recipients r in myCol) { var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new System.Net.NetworkCredential(myPrefs[0].EmailAddress.ToString(), myPrefs[0].Password.ToString()), EnableSsl = true }; client.Send(myPrefs[0].EmailAddress.ToString(), r.EmailAddress.ToString(), "Today's TV Shows for " + DateTime.Today.ToShortDateString(), body); } this.SplashScreenService.HideSplashScreen(); } }
public List <MimeMessage> FormMessages(RecipientsCollection recipients, Ebook ebook) { var activeRecipients = recipients.Recipients.Where(n => n.Active); return(activeRecipients.Select(n => FormSingleMessage(n, ebook)).ToList()); }
public RecipientsCollectionTests() { _collection = new RecipientsCollection(); }