Я не могу извлечь методы и типы из WSDL, созданного с помощью WCF - PullRequest
0 голосов
/ 11 ноября 2019

Я хочу извлечь методы и типы из wsdl, но проблема в том, что когда я тестирую wsdl, сгенерированный из службы WCF, мой код не работает.

C #

private void EValidateWsdlUrl(object obj)
{
    string Http_URL = HttpUtility.UrlDecode(UrlWsdl);

    try
    {
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(Http_URL);
        webRequest.Timeout = 36000;
        webRequest.UseDefaultCredentials = true;

        if (webRequest != null && webRequest.GetResponse().ContentLength > 0)
        {
            //webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            //webRequest.Method = "Get";
            //webRequest.Accept = "text/xml";

            //Submit a web request to get the web service's WSDL
            ServiceDescription serviceDescription;

            using (WebResponse response = webRequest.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    serviceDescription = ServiceDescription.Read(stream);
                }
            }

            foreach (PortType portType in serviceDescription.PortTypes)
            {
                foreach (System.Web.Services.Description.Operation operation in portType.Operations)
                {
                    Method method = new Method();
                    method.Name = operation.Name;
                    Console.Out.WriteLine(operation.Name);

                    foreach (var message in operation.Messages)
                    {
                        if (message is OperationInput)
                        {
                            method.Input.Add(((OperationInput)message).Message.Name);
                            Console.WriteLine("Input Message: {0}", ((OperationInput)message).Message.Name);
                        }

                        if (message is OperationOutput)
                        {
                            method.Output.Add(((OperationOutput)message).Name);
                            Console.WriteLine("Output Message: {0}", ((OperationOutput)message).Message.Name);
                        }

                        foreach (Message messagePart in serviceDescription.Messages)
                        {
                            if (messagePart.Name != ((OperationMessage)message).Name) continue;

                            foreach (MessagePart part in messagePart.Parts)
                            {
                                Console.WriteLine(part.Name);
                            }
                        }
                    }
                    soapObject.Methods.Add(method);
                    Console.WriteLine();
                }
            }


            //-----------------------------------------------------------------------second--------------------------------------------------------------------------------------
            //Drill down into the WSDL's complex types to list out the individual schema elements 
            //and their data types
            Types types = serviceDescription.Types;
            //var test = serviceDescription.Types.Schemas[0].Includes[2].SourceUri;

            var xmlSchema = types.Schemas[0];

            foreach (object item in xmlSchema.Items)
            {
                XmlSchemaElement schemaElement = item as XmlSchemaElement;
                XmlSchemaComplexType complexType = item as XmlSchemaComplexType;
                XmlSchemaSimpleType schemaSimpleElement = item as XmlSchemaSimpleType;

                if (schemaElement != null)
                {
                    Console.WriteLine("Schema Element: {0}", schemaElement.Name);

                    XmlSchemaType schemaType = schemaElement.SchemaType;
                    XmlSchemaComplexType schemaComplexType = schemaType as XmlSchemaComplexType;

                    if (schemaComplexType != null)
                    {
                        XmlSchemaParticle particle = schemaComplexType.Particle;
                        XmlSchemaSequence sequence = particle as XmlSchemaSequence;

                        if (sequence != null)
                        {
                            foreach (XmlSchemaElement childElement in sequence.Items)
                            {
                                Console.WriteLine("    Element/Type: {0}:{1}", childElement.Name, childElement.SchemaTypeName.Name);
                            }
                        }
                    }
                }
                else if (complexType != null)
                {
                    Console.Out.WriteLine("Complex Type: {0}", complexType.Name);
                    TypeInfo typeInfo = new TypeInfo(complexType.Name);
                    var type = OutputElements(complexType.Particle, typeInfo);
                    soapObject.Types.Add(type);
                }
                else if (schemaSimpleElement != null)
                {
                    //Type and  BaseType Definition
                    XmlSchemaSimpleTypeRestriction schemaSimpleTypeRestriction = (XmlSchemaSimpleTypeRestriction)schemaSimpleElement.Content;

                    Console.WriteLine("Type/BaseType Name: {0}:{1}", schemaSimpleElement.Name, schemaSimpleTypeRestriction.BaseTypeName.Name);
                    BaseType baseType = new BaseType();
                    baseType.BaseTypeName = new Tuple<string, string>(schemaSimpleElement.Name, schemaSimpleTypeRestriction.BaseTypeName.Name);
                    foreach (var facet in schemaSimpleTypeRestriction.Facets)
                    {
                        var result = Convert.ChangeType(facet, facet.GetType());
                        if (facet.GetType().Name.Equals("XmlSchemaEnumerationFacet"))
                            baseType.type = "Enum";
                        baseType.Rules.Add(new Dictionary<string, string>() { { result.GetType().Name, result.GetType().GetProperty("Value").GetValue(result, null).ToString() } });

                    }
                    BaseTypes.Add(baseType);
                }

                Console.WriteLine();
            }
        }
    }
    catch (Exception)
    {
        MessageBox.Show("false");
    }
}

//------------------------------------------------------------------------------------------------------------
private static TypeInfo OutputElements(XmlSchemaParticle particle, TypeInfo typeInfo)
{
    XmlSchemaSequence sequence = particle as XmlSchemaSequence;
    XmlSchemaChoice choice = particle as XmlSchemaChoice;
    XmlSchemaAll all = particle as XmlSchemaAll;

    if (sequence != null)
    {
        Console.WriteLine("  Sequence");

        for (int i = 0; i < sequence.Items.Count; i++)
        {
            XmlSchemaElement childElement = sequence.Items[i] as XmlSchemaElement;
            XmlSchemaSequence innerSequence = sequence.Items[i] as XmlSchemaSequence;
            XmlSchemaChoice innerChoice = sequence.Items[i] as XmlSchemaChoice;
            XmlSchemaAll innerAll = sequence.Items[i] as XmlSchemaAll;

            if (childElement != null)
            {
                typeInfo.Properties.Add(new TypeInfo(childElement.Name, childElement.SchemaTypeName.Name));
                Console.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                      childElement.SchemaTypeName.Name);
            }
            else typeInfo.Properties.Add(OutputElements(sequence.Items[i] as XmlSchemaParticle, new TypeInfo()));
        }
    }
    else if (choice != null)
    {
        Console.WriteLine("  Choice");
        for (int i = 0; i < choice.Items.Count; i++)
        {
            XmlSchemaElement childElement = choice.Items[i] as XmlSchemaElement;
            XmlSchemaSequence innerSequence = choice.Items[i] as XmlSchemaSequence;
            XmlSchemaChoice innerChoice = choice.Items[i] as XmlSchemaChoice;
            XmlSchemaAll innerAll = choice.Items[i] as XmlSchemaAll;

            if (childElement != null)
            {
                Console.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                      childElement.SchemaTypeName.Name);
            }
            else typeInfo.Properties.Add(OutputElements(choice.Items[i] as XmlSchemaParticle, new TypeInfo()));
        }

        Console.WriteLine();
    }
    else if (all != null)
    {
        Console.WriteLine("  All");
        for (int i = 0; i < all.Items.Count; i++)
        {
            XmlSchemaElement childElement = all.Items[i] as XmlSchemaElement;
            XmlSchemaSequence innerSequence = all.Items[i] as XmlSchemaSequence;
            XmlSchemaChoice innerChoice = all.Items[i] as XmlSchemaChoice;
            XmlSchemaAll innerAll = all.Items[i] as XmlSchemaAll;

            if (childElement != null)
            {
                Console.WriteLine("    Element/Type: {0}:{1}", childElement.Name,
                                      childElement.SchemaTypeName.Name);
            }
            else typeInfo.Properties.Add(OutputElements(all.Items[i] as XmlSchemaParticle, new TypeInfo()));
        }
        Console.WriteLine();
    }
    return typeInfo;
}
//enter code here
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...