我无法从WCF生成的wsdl中提取方法和类型

我想从wsdl中提取方法和类型,但是当我测试从WCF服务生成的wsdl时,我的代码不起作用。

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.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.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.SchemaTypeName.Name);
            }
            else typeInfo.Properties.Add(OutputElements(all.Items[i] as XmlSchemaParticle,new TypeInfo()));
        }
        Console.WriteLine();
    }
    return typeInfo;
}
//enter code here
cutmiss3 回答:我无法从WCF生成的wsdl中提取方法和类型

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3125206.html

大家都在问