public DiscoveryDocument DiscoverAny(string url)
        {
            Type[]             discoveryReferenceTypes = WebServicesSection.Current.DiscoveryReferenceTypes;
            DiscoveryReference reference   = null;
            string             contentType = null;
            Stream             stream      = this.Download(ref url, ref contentType);

            this.Errors.Clear();
            bool      flag           = true;
            Exception innerException = null;
            ArrayList list           = new ArrayList();

            foreach (Type type in discoveryReferenceTypes)
            {
                if (typeof(DiscoveryReference).IsAssignableFrom(type))
                {
                    reference                = (DiscoveryReference)Activator.CreateInstance(type);
                    reference.Url            = url;
                    reference.ClientProtocol = this;
                    stream.Position          = 0L;
                    Exception exception2 = reference.AttemptResolve(contentType, stream);
                    if (exception2 == null)
                    {
                        break;
                    }
                    this.Errors[type.FullName] = exception2;
                    reference = null;
                    InvalidContentTypeException exception3 = exception2 as InvalidContentTypeException;
                    if ((exception3 == null) || !ContentType.MatchesBase(exception3.ContentType, "text/html"))
                    {
                        flag = false;
                    }
                    if (exception2 is InvalidDocumentContentsException)
                    {
                        innerException = exception2;
                        break;
                    }
                    if ((exception2.InnerException != null) && (exception2.InnerException.InnerException == null))
                    {
                        list.Add(exception2.InnerException.Message);
                    }
                }
            }
            if (reference == null)
            {
                if (innerException != null)
                {
                    StringBuilder builder = new StringBuilder(Res.GetString("TheDocumentWasUnderstoodButContainsErrors"));
                    while (innerException != null)
                    {
                        builder.Append("\n  - ").Append(innerException.Message);
                        innerException = innerException.InnerException;
                    }
                    throw new InvalidOperationException(builder.ToString());
                }
                if (flag)
                {
                    throw new InvalidOperationException(Res.GetString("TheHTMLDocumentDoesNotContainDiscoveryInformation"));
                }
                bool flag2 = (list.Count == this.Errors.Count) && (this.Errors.Count > 0);
                for (int i = 1; flag2 && (i < list.Count); i++)
                {
                    if (((string)list[i - 1]) != ((string)list[i]))
                    {
                        flag2 = false;
                    }
                }
                if (flag2)
                {
                    throw new InvalidOperationException(Res.GetString("TheDocumentWasNotRecognizedAsAKnownDocumentType", new object[] { list[0] }));
                }
                StringBuilder builder2 = new StringBuilder(Res.GetString("WebMissingResource", new object[] { url }));
                foreach (DictionaryEntry entry in this.Errors)
                {
                    Exception exception5 = (Exception)entry.Value;
                    string    key        = (string)entry.Key;
                    if (string.Compare(key, typeof(ContractReference).FullName, StringComparison.Ordinal) == 0)
                    {
                        key = Res.GetString("WebContractReferenceName");
                    }
                    else if (string.Compare(key, typeof(SchemaReference).FullName, StringComparison.Ordinal) == 0)
                    {
                        key = Res.GetString("WebShemaReferenceName");
                    }
                    else if (string.Compare(key, typeof(DiscoveryDocumentReference).FullName, StringComparison.Ordinal) == 0)
                    {
                        key = Res.GetString("WebDiscoveryDocumentReferenceName");
                    }
                    builder2.Append("\n- ").Append(Res.GetString("WebDiscoRefReport", new object[] { key, exception5.Message }));
                    while (exception5.InnerException != null)
                    {
                        builder2.Append("\n  - ").Append(exception5.InnerException.Message);
                        exception5 = exception5.InnerException;
                    }
                }
                throw new InvalidOperationException(builder2.ToString());
            }
            if (reference is DiscoveryDocumentReference)
            {
                return(((DiscoveryDocumentReference)reference).Document);
            }
            this.References[reference.Url] = reference;
            DiscoveryDocument document = new DiscoveryDocument();

            document.References.Add(reference);
            return(document);
        }
Esempio n. 2
0
        public DiscoveryDocument DiscoverAny(string url)
        {
            Type[]             refTypes    = WebServicesSection.Current.DiscoveryReferenceTypes;
            DiscoveryReference discoRef    = null;
            string             contentType = null;
            Stream             stream      = Download(ref url, ref contentType);

            Errors.Clear();
            bool      allErrorsAreHtmlContentType = true;
            Exception errorInValidDocument        = null;
            ArrayList specialErrorMessages        = new ArrayList();

            foreach (Type type in refTypes)
            {
                if (!typeof(DiscoveryReference).IsAssignableFrom(type))
                {
                    continue;
                }
                discoRef                = (DiscoveryReference)Activator.CreateInstance(type);
                discoRef.Url            = url;
                discoRef.ClientProtocol = this;
                stream.Position         = 0;
                Exception e = discoRef.AttemptResolve(contentType, stream);
                if (e == null)
                {
                    break;
                }

                Errors[type.FullName] = e;
                discoRef = null;

                InvalidContentTypeException e2 = e as InvalidContentTypeException;
                if (e2 == null || !ContentType.MatchesBase(e2.ContentType, "text/html"))
                {
                    allErrorsAreHtmlContentType = false;
                }

                InvalidDocumentContentsException e3 = e as InvalidDocumentContentsException;
                if (e3 != null)
                {
                    errorInValidDocument = e;
                    break;
                }

                if (e.InnerException != null && e.InnerException.InnerException == null)
                {
                    specialErrorMessages.Add(e.InnerException.Message);
                }
            }

            if (discoRef == null)
            {
                if (errorInValidDocument != null)
                {
                    StringBuilder errorMessage = new StringBuilder(Res.GetString(Res.TheDocumentWasUnderstoodButContainsErrors));
                    while (errorInValidDocument != null)
                    {
                        errorMessage.Append("\n  - ").Append(errorInValidDocument.Message);
                        errorInValidDocument = errorInValidDocument.InnerException;
                    }
                    throw new InvalidOperationException(errorMessage.ToString());
                }
                else if (allErrorsAreHtmlContentType)
                {
                    throw new InvalidOperationException(Res.GetString(Res.TheHTMLDocumentDoesNotContainDiscoveryInformation));
                }
                else
                {
                    bool same = specialErrorMessages.Count == Errors.Count && Errors.Count > 0;
                    for (int i = 1; same && i < specialErrorMessages.Count; i++)
                    {
                        if ((string)specialErrorMessages[i - 1] != (string)specialErrorMessages[i])
                        {
                            same = false;
                        }
                    }
                    if (same)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.TheDocumentWasNotRecognizedAsAKnownDocumentType, specialErrorMessages[0]));
                    }
                    else
                    {
                        Exception     e;
                        StringBuilder errorMessage = new StringBuilder(Res.GetString(Res.WebMissingResource, url));
                        foreach (DictionaryEntry entry in Errors)
                        {
                            e = (Exception)(entry.Value);
                            string refType = (string)(entry.Key);
                            if (0 == string.Compare(refType, typeof(ContractReference).FullName, StringComparison.Ordinal))
                            {
                                refType = Res.GetString(Res.WebContractReferenceName);
                            }
                            else if (0 == string.Compare(refType, typeof(SchemaReference).FullName, StringComparison.Ordinal))
                            {
                                refType = Res.GetString(Res.WebShemaReferenceName);
                            }
                            else if (0 == string.Compare(refType, typeof(DiscoveryDocumentReference).FullName, StringComparison.Ordinal))
                            {
                                refType = Res.GetString(Res.WebDiscoveryDocumentReferenceName);
                            }
                            errorMessage.Append("\n- ").Append(Res.GetString(Res.WebDiscoRefReport,
                                                                             refType,
                                                                             e.Message));
                            while (e.InnerException != null)
                            {
                                errorMessage.Append("\n  - ").Append(e.InnerException.Message);
                                e = e.InnerException;
                            }
                        }
                        throw new InvalidOperationException(errorMessage.ToString());
                    }
                }
            }

            if (discoRef is DiscoveryDocumentReference)
            {
                return(((DiscoveryDocumentReference)discoRef).Document);
            }

            References[discoRef.Url] = discoRef;
            DiscoveryDocument doc = new DiscoveryDocument();

            doc.References.Add(discoRef);
            return(doc);
        }