private Lab GetLab(string uri) { Uri theUri; if (!Uri.TryCreate(uri, UriKind.Absolute, out theUri)) { // It may be missing the protocol. ie) http:// or https:// var withProtocol = $"http://{uri}"; if (Uri.IsWellFormedUriString(withProtocol, UriKind.Absolute)) { theUri = new Uri(withProtocol); } else { throw new ArgumentException($"{nameof(uri)}: Could not parse the input into a proper URI [{uri}]"); } } var fullUri = theUri.ToString(); Lab lab; if (_labs.TryGetValue(fullUri, out lab)) { return lab; } lab = new Lab(theUri); // Dictionary will always update the value with the latest value. _labs.AddOrUpdate(fullUri, _ => lab, (key, _) => lab); return lab; }
static void Main(string[] args) { var uri = "http://www.med.umich.edu/lrc/coursepages/m1/anatomy2010/html/quizzes/practical2007/cardio_respiratory/lab14.html"; Lab lab; try { lab = new Lab(new Uri(uri)); } catch (ArgumentException e) { Console.WriteLine(e); return; } var answer = lab.GetAnswerAsync(8).Result; var answer2 = lab.GetAnswerAsync(9).Result; }