我使用ServiceDescription / ServiceDescriptionImporter类来动态调用Web服务。我想深入研究WSDL描述并获取
1)每个Web方法的参数信息
2)所有Web方法的每个参数的实际类型/组成(即,如果WebMethod将某些复杂类型作为参数,我需要知道它组成的原语/其他类型,如果可能的话)< / p>
以下是动态调用的代码:
public static object CallWebService(string webServiceAsmx, string serviceName, string methodName, object[] args = null)
{
WebClient client = new WebClient();
Stream stream = client.OpenRead(webServiceAsmx + "?wsdl");
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap12";
importer.AddServiceDescription(description, null, null);
importer.Style = ServiceDescriptionImportStyle.Client;
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
我已经能够找到一些基本信息,如方法名称,参数信息,但我需要更深入的分析。例如,我需要访问Wsdl.exe在代理类中生成的基本上所有信息,但我不想运行Wsdl.exe,只是动态发现信息。对于每个方法,我需要知道它的返回类型是由什么组成的,它的参数是由什么组成的等等。我知道它在WSDL中只是不确定如何以编程方式提取它。以下是我一直在探索的一些课程:
ServiceDescription.Description
ServiceDescription.Messages
ServiceDescription.Types
看来很多人都空了?
提前致谢。
EDITS
我进一步说,这是我试图遍历的xml架构(WSDL):
- <s:complexType name="Session">
- <s:complexContent mixed="false">
- <s:extension base="tns:EntityObject">
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Host" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="SessionType" type="s:string" />
<s:element minOccurs="1" maxOccurs="1" name="LoginTime" type="s:dateTime" />
<s:element minOccurs="1" maxOccurs="1" name="LogoutTime" type="s:dateTime" />
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
这是遍历的代码:
foreach (System.Xml.Schema.XmlSchemaComplexType item in xmlSchema.SchemaTypes.Values)
{
if (item != null)
{
System.Xml.Schema.XmlSchemaParticle particle = item.Particle;
System.Xml.Schema.XmlSchemaSequence sequence = particle as System.Xml.Schema.XmlSchemaSequence;
if (sequence != null)
{
foreach (System.Xml.Schema.XmlSchemaElement childElement in sequence.Items)
{
string name = childElement.Name;
string type = childElement.SchemaTypeName.Name;
Console.WriteLine(name + " " + type);
}
}
}
这让我更进一步,但不是获得ComplexType的complexContent。在控制台中生成以下输出:
会话会议
答案 0 :(得分:1)
如果有人有类似的问题,我就是这样做的:
foreach (System.Xml.Schema.XmlSchemaComplexType item in xmlSchema.SchemaTypes.Values)
{
ComplexType cType = new ComplexType(item.Name);
System.Xml.Schema.XmlSchemaContentModel model = item.ContentModel;
System.Xml.Schema.XmlSchemaComplexContent complex = model as System.Xml.Schema.XmlSchemaComplexContent;
if (complex != null)
{
System.Xml.Schema.XmlSchemaComplexContentExtension extension = complex.Content as System.Xml.Schema.XmlSchemaComplexContentExtension;
System.Xml.Schema.XmlSchemaParticle particle = extension.Particle;
System.Xml.Schema.XmlSchemaSequence sequence = particle as System.Xml.Schema.XmlSchemaSequence;
if (sequence != null)
{
List<SimpleType> primitives = new List<SimpleType>();
foreach (System.Xml.Schema.XmlSchemaElement childElement in sequence.Items)
{
string name = childElement.Name;
string type = childElement.SchemaTypeName.Name;
cType.Primitives.Add(new SimpleType(name, type));
}
if (cType.Name == parameter.Type || "ArrayOf" + cType.Name == parameter.Type)
{
descriptions.Add(new ComplexParameter(cType, item.Name));
}
}
}
}
答案 1 :(得分:0)
这与你所做的非常相似WebServiceInfo它返回带有信息的自定义对象。 (如果我理解你的问题和博客足够好)