2009-02-04 34 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'子句使它像魅力一样工作。

对不起,我感到困惑。

相关问题