gpt4 book ai didi

.net - 如何使用 svcutil 从使用限制来隐藏元素的 Web 服务生成 C# WCF 代理?

转载 作者:行者123 更新时间:2023-12-03 17:58:09 24 4
gpt4 key购买 nike

我正在为或多或少不受我控制的 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>

简而言之,我们有一个包含所有元素的对象 A。该服务有几种基于 A 的类型,但有一些限制,因此继承的类型通常小于基本类型——这里以类型 B 为例。

在像 Visual Studio 2010、SoapUI 等中的架构查看器中,这看起来像预期的那样。 A 有 2 个元素,B 只有 1 个(= 元素 2)。

通过使用 svcutil,我获得了类型 A 和 B 中的完整元素集,或者在使用选项时我收到错误消息,例如:

Error: Type 'B' in namespace 'http://tempuri.org/XMLSchema.xsd' cannot be imported. Complex types derived by restriction not supported. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.



在继承类型中隐藏字段/属性不是我喜欢旅行的实践/道路,但如果我无法让提供者更改 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;
}
}

在 Xml 级别上工作是不可取的,并且会迫使我们编写包装器。从 getgo 手动编码代理更容易。

svcutil schema.xsd/serializer:XmlSerializer/datacontractonly 给出了下面的错误,这就是我要求替代工具的原因。

svcutil schema.xsd /serializer:XmlSerializer /datacontractonly Error: Type 'B' in namespace 'http://tempuri.org/XMLSchema.xsd' cannot be imported. Complex types derived by restriction not supported. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.

If you are using the /dataContractOnly option to import data contract types and are getting this error message, consider using xsd.exe instead. Types generated by xsd.exe may be used in the Windows Communication Foundation after applying the XmlSerializerFormatAttribute attribute on your service contract. Alternatively, consider using the /importXmlTypes option to import these types as XML types to use with DataContractFormatAttribute attribute on your service contract.



使用 xsd 架构.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;
}
}
}

最佳答案

错误消息告诉您要么使用 /importXmlTypes切换,或更改为使用 XmlSerializer。从帮助:

/importXmlTypes - Configure the Data Contract serializer to import non-Data Contract types as IXmlSerializable types.





/serializer:XmlSerializer - Generate data types that use the XmlSerializer for serialization and deserialization

关于.net - 如何使用 svcutil 从使用限制来隐藏元素的 Web 服务生成 C# WCF 代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7861207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com