gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:52:47 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com