在WCF,可以数据合同类相互继承?同类、数据、WCF

2023-09-02 10:46:54 作者:我需要的不只是妳的關心

在一个WCF服务,我有两个班的[DataContract]属性。其中一个班都有一个是一个与其他的关系 - 所以B类可以从类A继承然而,当我配置这两个类之间的继承,既表示了[DataContract]属性,元数据加载失败测试服务时。

In a WCF service, I have two classes with the [DataContract] attribute. One of these classes has an "is-a" relationship with the other - so class B can inherit from class A. However, when I configure inheritance between these two classes, both denoted with a [DataContract] attribute, the metadata fails to load when testing the services.

这是有可能在WCF?我失去了另一个属性?

Is this possible in WCF? Am I missing another attribute?

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

注:自定义类型是使用数据契约还规定

NOTE: The custom types are also defined using data contracts.

更新:下面是错误:

错误://本地主机:无法从 HTTP获得8002元/ GISDataServices / MEX 如果这是一个Windows(R)通信基础服务,您可以访问,请检查您是否已经启用了元数据发布在指定的地址。为了帮助实现元数据发布,请参阅MSDN文档的http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange错误URI:的http://本地主机:8002 / GISDataServices / MEX 元数据包含了一个参考无法得到解决:HTTP:/ /本地主机:8002 / GISDataServices / MEX。 Receivera:InternalServiceFault 服务器无法处理请求由于内部错误。有关错误的上IncludeExceptionDetailInFaults(无论是从ServiceBehaviorAttribute或从在< serviceDebug>配置行为)的详细信息,或者反过来在服务器上以发送异常信息返回给客户端,或打开跟踪按照微软.NET框架3.0 SDK文档,检查服务器跟踪logs.HTTP得到错误的URI://本地主机:8002 / GISDataServices / MEX 有 HTTP一个错误下载'的http://本地主机:8002 / GISDataServices / MEX。请求失败,HTTP状态400:错误的请求

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:InternalServiceFaultThe 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:查看我的回答如下

推荐答案

是的,但你需要跟装饰基类中的[KnownTypeAttribute]与派生类的类型构建它。例如:

Yes, but you need to decorate the base class with the [KnownTypeAttribute] constructing it with the derived class's type. For instance:

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

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