首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用svcutil从使用限制来隐藏元素的web服务生成C# WCF代理?

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

Stack Overflow用户
提问于 2011-10-23 01:11:42
回答 1查看 8K关注 0票数 5

我正在创建一个或多或少超出我控制范围的web服务的客户端。以下是该模式的简化示例:

代码语言:javascript
复制
<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 Studio2010、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结果如下:

代码语言:javascript
复制
[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错误:无法导入命名空间'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:

代码语言:javascript
复制
[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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-10-23 02:27:03

错误消息告诉您要么使用/importXmlTypes开关,要么更改为使用XmlSerializer。从帮助中:

/importXmlTypes -配置数据协定序列化程序以将非数据协定类型作为IXmlSerializable类型导入。

/serializer:XmlSerializer -生成使用XmlSerializer进行序列化和反序列化

的数据类型

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7861207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档