我正在尝试导入WCF服务操作列表。我需要获得的是每个操作的名称,它的参数名称和类型以及结果类型。
我正在测试它的服务很简单,只有一个操作接受int类型的参数。
WSDL:
<wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://localhost:3657/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://localhost:3657/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IService1_GetData_InputMessage">
<wsdl:part name="parameters" element="tns:GetData"/>
</wsdl:message><wsdl:message name="IService1_GetData_OutputMessage">
<wsdl:part name="parameters" element="tns:GetDataResponse"/>
</wsdl:message>
<wsdl:portType name="IService1">
<wsdl:operation name="GetData">
<wsdl:input wsaw:Action="http://tempuri.org/IService1/GetData" message="tns:IService1_GetData_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataResponse" message="tns:IService1_GetData_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IService1" type="tns:IService1">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetData">
<soap:operation soapAction="http://tempuri.org/IService1/GetData" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service1">
<wsdl:port name="BasicHttpBinding_IService1" binding="tns:BasicHttpBinding_IService1">
<soap:address location="http://localhost:3657/Service1.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
xsd0看起来像这样:
<xs:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<xs:element name="GetData">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="value" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetDataResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="GetDataResult" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我正在使用这样的System.ServiceModel.Description.WsdlImporter类:
MetadataExchangeClient mexClient = new MetadataExchangeClient(
new Uri("http://localhost:3657/Service1.svc?singleWsdl"),
MetadataExchangeClientMode.HttpGet);
mexClient.ResolveMetadataReferences = true;
MetadataSet metaDocs = mexClient.GetMetadata();
WsdlImporter importer = new WsdlImporter(metaDocs);
接下来,我将收到合同清单并通过它进行迭代:
Collection<ContractDescription> contracts = importer.ImportAllContracts();
foreach (ContractDescription contract in contracts)
{
Debug.WriteLine("Contract name:" + contract.Name);
foreach (OperationDescription operation in contract.Operations)
{
Debug.WriteLine("\tOperation name:" + operation.Name);
foreach (MessageDescription message in operation.Messages)
{
MessageBodyDescription body = message.Body;
Debug.WriteLine("\t\t\tMessage wrapper name: " + body.WrapperName);
foreach (MessagePartDescription part in body.Parts)
{
Debug.WriteLine("\t\t\t\tPart name: " + part.Name);
Debug.WriteLine("\t\t\t\tPart type: " + part.Type);
}
if (body.ReturnValue != null)
{
Debug.WriteLine("\t\t\t\tReturn value type: " + body.ReturnValue.Type);
}
}
}
}
结果应该是打印的操作信息。我得到以下输出:
Contract name: IService1 Operation name: GetData Message wrapper name: GetData Part name: value Part type: Message wrapper name: GetDataResponse Return value type:
如您所见,对应于操作参数的部分在其Type属性中包含null。同样,返回值类型也为null。
我检查了合同集合的内容,并且在每个应该有类型信息的地方,它都是空的(如OperationDescription.KnownTypes属性)。
我尝试了this问题的方法,但它失败了:
foreach (object item in xmlSchema.Items)
其中Items集合为空。
有谁知道,为什么缺少类型信息?其他一切似乎都存在。
感谢。
编辑:我发现类型信息存在于邮件正文部分&#34; BaseType&#34;的非公共属性中。为什么这是私人的?有没有办法访问? (不涉及创建一个实际的代理,我现在不想做。)