.net - 在 WCF 中,数据协定类可以相互继承吗?

标签 .net wcf contract

在 WCF 服务中,我有两个具有 [DataContract] 属性的类。其中一个类与另一个类具有“is-a”关系 - 因此类 B 可以从类 A 继承。但是,当我在这两个类之间配置继承时,都用 [DataContract] 属性表示,元数据无法加载在测试服务时。

这在 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; }
}

注意:自定义类型也是使用数据协定定义的。

更新 : 下面是错误:

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] 来装饰基类,并使用派生类的类型构造它。例如:

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

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

关于.net - 在 WCF 中,数据协定类可以相互继承吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/511793/

相关文章:

C#:InvalidCastException 误导性消息背后的原因

c# - 无法解释的 OverflowException 将表示屏幕坐标的 IntPtr 转换为 Int32

c# - C# 中的接口(interface)继承 : property does not inherit get accessor

c# - namespace 在项目中不可见。 (命名空间中不存在类型或命名空间名称)

c# - 类型 'System.Int32' 的表达式不能用于方法 'System.Object' 的类型 'Boolean Equals(System.Object)' 的参数

wcf - 我可以防止特定数据成员被反序列化吗?

wcf - 无法通过邮件到达 WCF 休息端点

java - 字符串元素内的 XML 与独立元素

testing - 使用js的truffle测试, `contract `后面的参数怎么设置?

blockchain - 从外部合约调用时,solidity 合约状态变量值显示奇怪