WCF中继承DataContract

发布于 2025-01-05 12:14:56 字数 419 浏览 2 评论 0原文

我有一个名为 CredentialsDataContract,我已将其继承到我自己的名为 MyCredentials 的业务对象中并进行了自定义。我想通过网络发送它,但可以理解的是我收到了错误。

尝试序列化参数时出错

有没有办法在不进行业务对象和 DataContracts 之间转换的情况下解决此问题?我的代码工作看起来像这样

[DataContract]
[KnownType(typeof(Credentials))]
internal class MyCredentials : Credentials
{
    public MyCredentials ()
    {
    }
}

I have a DataContract say called Credentials which I've inherited into my own business object called MyCredentials and customized. I want to send it over the wire but understandably I get an error.

There was an error while trying to serialize parameter

Is there a way to resolve this without doing a conversion between business object and DataContracts? My code work looks something like this

[DataContract]
[KnownType(typeof(Credentials))]
internal class MyCredentials : Credentials
{
    public MyCredentials ()
    {
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2025-01-12 12:14:56

您需要使用 KnownType-Attribute 来修饰基类 Credentials。

[DataContract]
[KnownType(typeof(MyCredentials))]
publice class Credentials
{
    public Credentials()
    {
    }
}

另外,我认为当您使用 DataContractAttribute 装饰该类时,需要将其设为公共类而不是内部类。

[DataContract]
public class MyCredentials : Credentials {
  public MyCredentials () {
  }
}

You need to decorate the base class Credientials with the KnownType-Attribute.

[DataContract]
[KnownType(typeof(MyCredentials))]
publice class Credentials
{
    public Credentials()
    {
    }
}

Also i think you need to make the class public and not internal when you decorate it with the DataContractAttribute.

[DataContract]
public class MyCredentials : Credentials {
  public MyCredentials () {
  }
}
享受孤独 2025-01-12 12:14:56

毕竟它是一个契约,可访问性修饰符不会影响 DataContract。

Accessibility modifiers do not affect a DataContract after all it is a contract.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文