2009-02-04 67 views
24

在WCF服務中,我有兩個具有[DataContract]屬性的類。其中一個類與另一個類具有「是-α」關係 - 因此B類可以從類A繼承。但是,當我在這兩個類之間配置繼承時,它們都用[DataContract]屬性表示,元數據無法加載在測試服務時。在WCF中,數據約定類可以相互繼承嗎?

這是可能的WCF?我錯過了另一個屬性?

[DataContract] 
public class A 
{   
    [DataMember] 
    public MyCustomType AValue1{ get; set; } 

    [DataMember] 
    public MyCustomType AValue2 { get; set; } 
} 

[DataContract] 
public class B: A 
{  
    [DataMember] 
    public double BValue1{ get; set; } 

    [DataMember] 
    public double BValue2 { get; set; } 
} 

注意:自定義類型也使用數據合同進行定義。

UPDATE:下面是錯誤:

Error: Cannot obtain Metadata from http://localhost:8002/GISDataServices/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:8002/GISDataServices/mex Metadata contains a reference that cannot be resolved: ' http://localhost:8002/GISDataServices/mex '. Receivera:InternalServiceFault The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.HTTP GET Error URI: http://localhost:8002/GISDataServices/mex There was an error downloading ' http://localhost:8002/GISDataServices/mex '. The request failed with HTTP status 400: Bad Request.

更新2:見我的回答如下。

回答

38

是的,但是您需要使用[KnownTypeAttribute]構造基類並使用派生類的類型來修飾基類。例如:

[DataContract] 
[KnownType(typeof(B))] 
public class A 
{ 
    [DataMember] 
    public string Value { get; set; } 
} 

[DataContract] 
public class B : A 
{ 
    [DataMember] 
    public string OtherValue { get; set; } 
} 
+0

這似乎並不奏效。加載元數據時仍然出現錯誤。 – 2009-02-04 15:32:56

+0

你看到什麼錯誤? – 2009-02-04 15:49:14

1

基於這個測試它應該可以正常工作。這兩個類都有默認構造函數嗎?你在使用自動屬性嗎?請注意,在這個基本示例中,屬性不是必需的。此外,正如大衛莫頓提到你取決於你要返回哪個元素,你可能需要KnownType屬性,我不是100%,但已知類型可能不得不繼續運營合同。

class Program 
{ 
    static void Main(string[] args) 
    { 
     var serializer = new DataContractSerializer(typeof(Employee)); 

     var employee = new Employee() { Name="Joe", Salary=100000 }; 
     using (var ms = new MemoryStream()) 
     { 
      serializer.WriteObject(ms, employee); 

      ms.Position = 0; 

      var newEmployee = serializer.ReadObject(ms) as Employee; 
     } 

     Console.ReadKey(); 

    } 
} 

[DataContract] 
public class Employee : Person 
{ 
    [DataMember] 
    public decimal Salary { get; set; } 
} 

[DataContract] 
public class Person 
{ 
    [DataMember] 
    public string Name { get; set; } 
} 

[ServiceContract] 
interface IEmployeeService 
{ 
    [OperationContract] 
    Person GetPerson(); 

    [OperationContract] 
    Employee GetEmployee(); 

    [OperationContract] 
    [KnownType(typeof(Employee))] 
    Person GetEmployeeAsPerson(); 
} 
7

好的,我想出了問題。答案是......我是個白癡。它與繼承無關。在基類中,我有一個沒有'set'屬性子句的數據契約成員 - 只有'get'。衛生署!放入一個'set'子句使它像魅力一樣工作。

對不起,我感到困惑。