2011-10-22 31 views
5

Tôi đang tạo một ứng dụng khách cho một dịch vụ web ít nhiều trong tầm kiểm soát của tôi. Dưới đây là mẫu giản đồ được đơn giản hóa:Làm thế nào để sử dụng svcutil để tạo một proxy C# WCF từ một dịch vụ web sử dụng hạn chế để ẩn các phần tử?

<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> 

Tóm lại, chúng tôi có đối tượng A chứa tất cả các thành phần. Dịch vụ có một số loại dựa trên A nhưng có các hạn chế để các loại được kế thừa thường nhỏ hơn loại cơ sở - tại đây được minh họa bằng loại B.

Trong trình xem giản đồ như trong Visual Studio 2010, SoapUI, v.v. điều này có vẻ như mong đợi. A có 2 phần tử và B chỉ 1 (= phần tử 2).

Bằng cách sử dụng svcutil tôi nhận được tập hợp đầy đủ các yếu tố trong cả hai loại của tôi Một & B, hoặc khi chơi với các tùy chọn tôi nhận được thông báo lỗi như:

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.

Ẩn ruộng/tài sản trong các loại di truyền là không phải là một thực tế/đường tôi thích đi du lịch nhưng nếu tôi không thể có được nhà cung cấp để thay đổi WSDL có vẻ như tôi phải làm theo cách đó.

Có các lựa chọn thay thế cho svcutil xử lý đúng cách này hay tôi có phải viết mã proxy của mình không?


Cập nhật 1

Như đã chỉ ra bởi John Saunders Tôi đã không được hiển thị các kết quả của những lời đề nghị từ svcutil. Đó cũng là một phần để giữ cho các bài ngắn ... nhưng ở đây đi:

svcutil schema.xsd/importXmlTypes/datacontractonly kết quả trong:

[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; 
    } 
} 

Làm việc trên một Xml cấp không phải là mong muốn và sẽ buộc chúng tôi viết một trình bao bọc. Nó dễ dàng hơn để mã hóa proxy từ getgo.

svcutil schema.xsd/serializer: XmlSerializer/datacontractonly Cung cấp lỗi bên dưới và là lý do tôi yêu cầu các công cụ thay thế.

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.

Sử dụng xsd schema.xsd/c đưa ra một loại B mà được thừa hưởng một không giấu element1:

[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; 
     } 
    } 
} 
+0

gì một phần của thông báo lỗi không có hiểu không? Nó nói với bạn chính xác làm thế nào để giải quyết vấn đề. –

+2

Không có nó không cho tôi kết quả mong muốn (cũng lưu ý tôi đã nói những thông điệp như - nó chỉ là một mẫu - một người nghèo mà tôi biết ...). Nếu bạn đã thử sử dụng các tùy chọn đó, bạn sẽ thấy rằng nó không tạo ra một proxy với các thuộc tính có liên quan.Kết quả là tôi kết thúc với việc phải đối phó với danh sách các yếu tố và serializtion trực tiếp. –

+1

Tôi không quan tâm đến việc thử các tùy chọn đó. _You_ đã thử họ và _you_ biết kết quả. Vui lòng ** hiển thị ** cho chúng tôi những gì bạn đã thử và ** hiển thị cho chúng tôi kết quả. –

Trả lời

0

Các thông báo lỗi là nói cho bạn hoặc sử dụng /importXmlTypes chuyển đổi, hoặc thay đổi để sử dụng XmlSerializer. Từ sự giúp đỡ:

/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

+2

Có, tôi biết nhưng làm như vậy không cho tôi kết quả mong muốn của việc có loại B chỉ bao gồm thuộc tính có tên là element2. –

+0

"Làm như vậy?" Làm điều gì trong số những thứ tôi đã cho bạn thấy? Thế kết quả là gì? Cập nhật câu hỏi của bạn với nghiên cứu bạn đã làm và cho chúng tôi biết kết quả là gì. –