.net 如何使用svcutil从使用限制隐藏元素的web服务生成C#WCF代理?
我正在为一个或多或少超出我控制范围的web服务创建一个客户端。以下是模式的简化示例:.net 如何使用svcutil从使用限制隐藏元素的web服务生成C#WCF代理?,.net,web-services,wsdl,schema,svcutil.exe,.net,Web Services,Wsdl,Schema,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="elem
<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>
在Xml级别上工作是不可取的,这将迫使我们编写包装器。从getgo手动编写代理更容易
svcutil schema.xsd/serializer:XmlSerializer/datacontractonly给出了下面的错误,这也是我要求使用其他工具的原因
svcutil schema.xsd/serializer:XmlSerializer/datacontractonly
错误:在命名空间中键入“B”http://tempuri.org/XMLSchema.xsd“不行
可能是进口的。不支持由限制派生的复杂类型。
或者更改模式,以便类型可以映射到数据协定
类型或使用ImportXmlType或使用其他序列化程序
如果使用/dataContractOnly选项导入数据契约
类型并正在获取此错误消息,请考虑使用XSD.EXE
相反由xsd.exe生成的类型可以在Windows中使用
应用后的通信基础
服务合约上的XmlSerializerFormatAttribute属性。
或者,考虑使用//CuxMLMyType选项导入
这些类型作为XML类型与DataContractFormatAttribute一起使用
服务合同中的属性
使用xsd schema.xsd/c给出了一个类型B,它继承了a而不隐藏元素1:
[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;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute(“请求”,命名空间=”http://tempuri.org/XMLSchema.xsd“,IsNullable=false)]
公共部分B类:A{
}
///
[System.Xml.Serialization.xmlcludeAttribute(typeof(B))]
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://tempuri.org/XMLSchema.xsd")]
公共部分A类{
私有字符串元素1字段;
私有字符串元素2字段;
///
公共字符串元素1{
得到{
返回此.element1字段;
}
设置{
this.element1Field=值;
}
}
///
公共字符串元素2{
得到{
返回此.element2字段;
}
设置{
this.element2Field=值;
}
}
}
错误消息告诉您要么使用/importXmlTypes
开关,要么改为使用XmlSerializer。从帮助中:
/importXmlTypes-配置数据协定
用于导入非数据协定类型的序列化程序
作为IXmlSerializable类型
及
/serializer:XmlSerializer-生成使用
XmlSerializer用于序列化和
反序列化
你不明白错误信息的哪一部分?它确切地告诉了你如何解决这个问题。不,它没有给我想要的结果(还请注意,我说的消息,如-这只是一个样本-我知道的一个很差的…)。如果您尝试使用这些选项,您将看到它不会生成具有相关属性的代理。结果是我不得不直接处理元素列表和序列化,我对尝试这些选项没有兴趣。你已经试过了,你知道结果。请告诉我们您尝试了什么,并告诉我们结果。是的,我知道,但这样做并不会给我想要的结果,即B型仅由名为element2的属性组成。“这样做?”做我向您展示的两件事中的哪一件?结果如何?根据您所做的研究更新您的问题,并向我们展示结果。
[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;
}
}
}