Tro*_*kog 5 .net schema wsdl web-services svcutil.exe
我正在为一个或多或少不受我控制的Web服务创建一个客户端.以下是架构的简化示例:
<xs:complexType name="A">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="element1" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="element2" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="B">
<xs:complexContent>
<xs:restriction base="A">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="element2" type="xs:string" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
简而言之,我们有一个包含所有元素的对象A. 该服务有几种基于A但有限制的类型,因此继承的类型通常小于基类型 - 这里以类型B为例.
在Visual Studio 2010,SoapUI等中的模式查看器中,这看起来像预期的那样.A有2个元素,B只有1个(=元素2).
通过使用svcutil我可以在我的类型A和B中获得完整的元素集,或者在使用选项时我会收到错误消息,例如:
错误:无法导入名称空间"http://tempuri.org/XMLSchema.xsd"中的"B"类型.不受支持派生的复杂类型.更改架构以便类型可以映射到数据协定类型或使用ImportXmlType或使用其他序列化程序.
隐藏继承类型中的字段/属性不是我喜欢旅行的练习/道路,但如果我无法让提供者更改WSDL,那么我似乎必须这样做.
有没有替代svcutil正确处理这个或我必须手工编码我的代理?
更新1
正如John Saunders所指出的,我没有展示svcutil的建议结果.这部分是为了保持这个职位的简短......但是这里有:
svcutil schema.xsd/importXmlTypes/datacontractonly导致:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="A", Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class A : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private string element1Field;
private string element2Field;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
get
{
return this.extensionDataField;
}
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string element1
{
get
{
return this.element1Field;
}
set
{
this.element1Field = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string element2
{
get
{
return this.element2Field;
}
set
{
this.element2Field = value;
}
}
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
[System.Xml.Serialization.XmlRootAttribute(IsNullable=false)]
public partial class B : object, System.Xml.Serialization.IXmlSerializable
{
private System.Xml.XmlNode[] nodesField;
private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName("B", "http://tempuri.org/XMLSchema.xsd");
public System.Xml.XmlNode[] Nodes
{
get
{
return this.nodesField;
}
set
{
this.nodesField = value;
}
}
public void ReadXml(System.Xml.XmlReader reader)
{
this.nodesField = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
}
public void WriteXml(System.Xml.XmlWriter writer)
{
System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
}
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public static System.Xml.XmlQualifiedName ExportSchema(System.Xml.Schema.XmlSchemaSet schemas)
{
System.Runtime.Serialization.XmlSerializableServices.AddDefaultSchema(schemas, typeName);
return typeName;
}
}
Run Code Online (Sandbox Code Playgroud)
在Xml级别上工作是不可取的,会迫使我们编写一个包装器.从getgo手动编码代理更容易.
svcutil schema.xsd/serializer:XmlSerializer/datacontractonly给出下面的错误,这就是我要求替代工具的原因.
svcutil schema.xsd/serializer:XmlSerializer/datacontractonly错误:无法导入名称空间"http://tempuri.org/XMLSchema.xsd"中的"B"类型.不受支持派生的复杂类型.更改架构以便类型可以映射到数据协定类型或使用ImportXmlType或使用其他序列化程序.
如果您使用/ dataContractOnly选项导入数据协定类型并收到此错误消息,请考虑使用xsd.exe.在服务协定上应用XmlSerializerFormatAttribute属性后,可以在Windows Communication Foundation中使用xsd.exe生成的类型.或者,可以考虑使用/ importXmlTypes选项将这些类型作为XML类型导入,以与服务契约中的DataContractFormatAttribute属性一起使用.
使用xsd schema.xsd/c给出了一个继承A而不隐藏element1的类型B:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute("request", Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class B : A {
}
/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(B))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class A {
private string element1Field;
private string element2Field;
/// <remarks/>
public string element1 {
get {
return this.element1Field;
}
set {
this.element1Field = value;
}
}
/// <remarks/>
public string element2 {
get {
return this.element2Field;
}
set {
this.element2Field = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
该错误消息告诉您要么使用该/importXmlTypes开关,要么更改为使用 XmlSerializer。来自帮助:
/importXmlTypes - 配置数据协定序列化程序以将非数据协定类型导入为 IXmlSerialized 类型。
和
/serializer:XmlSerializer - 生成使用 XmlSerializer 进行序列化和反序列化的数据类型
| 归档时间: |
|
| 查看次数: |
7326 次 |
| 最近记录: |