public override async Task <Document> LoadDocumentAsync(DocumentSettings settings, DocumentInfo?sourceInfo, string specifier, DocumentCategory category, DocumentContextCallback contextCallback) { MiscHelpers.VerifyNonNullArgument(settings, "settings"); MiscHelpers.VerifyNonBlankArgument(specifier, "specifier", "Invalid document specifier"); if ((settings.AccessFlags & DocumentAccessFlags.EnableAllLoading) == DocumentAccessFlags.None) { throw new UnauthorizedAccessException("This script engine is not configured for loading documents"); } if (category == null) { category = sourceInfo.HasValue ? sourceInfo.Value.Category : DocumentCategory.Script; } List <Uri> candidateUris; Uri uri; if (Uri.TryCreate(specifier, UriKind.RelativeOrAbsolute, out uri) && uri.IsAbsoluteUri) { candidateUris = await GetCandidateUrisAsync(settings, sourceInfo, uri).ConfigureAwait(false); } else { candidateUris = await GetCandidateUrisAsync(settings, sourceInfo, specifier).ConfigureAwait(false); } if (candidateUris.Count < 1) { throw new FileNotFoundException(null, specifier); } if (candidateUris.Count == 1) { return(await LoadDocumentAsync(settings, candidateUris[0], category, contextCallback).ConfigureAwait(false)); } var exceptions = new List <Exception>(candidateUris.Count); foreach (var candidateUri in candidateUris) { var task = LoadDocumentAsync(settings, candidateUri, category, contextCallback); try { return(await task.ConfigureAwait(false)); } catch (Exception exception) { if ((task.Exception != null) && task.Exception.InnerExceptions.Count == 1) { Debug.Assert(ReferenceEquals(task.Exception.InnerExceptions[0], exception)); exceptions.Add(exception); } else { exceptions.Add(task.Exception); } } } if (exceptions.Count < 1) { MiscHelpers.AssertUnreachable(); throw new FileNotFoundException(null, specifier); } if (exceptions.Count == 1) { MiscHelpers.AssertUnreachable(); throw new FileLoadException(exceptions[0].Message, specifier, exceptions[0]); } throw new AggregateException(exceptions).Flatten(); }