传递继承的“数据合同";通过WCF调用? [英] Passing an inherited "Data Contract" through WCF call?

查看:92
本文介绍了传递继承的“数据合同";通过WCF调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WCF端点之一具有此方法:

One of my WCF endpoints has this method:

GetData(DataTable dt)

我试图在客户端上创建一个继承自DataTable类的类

I tried to create a class on the client that inherits from the DataTable class

public class ExtendedDataTable : DataTable{
  //...implementation
}

并将其与端点调用一起传递:

and pass it along with the endpoint call:

GetData(new ExtendedDataTable());

然后我得到了 SerializationException .相应于该错误,它建议我使用 DataContractResolver KnownType 属性.

Then I got the SerializationException. Accordingly to the error, it suggests that I use either DataContractResolver or the KnownType attribute.

我不想使用KnownType,因为我不必每次有人决定继承我的DataContract时都更新端点.我无法编写任何DataContractResolver,因为我没有扩展DataTable类的确切结构. 是否可以从客户端扩展DataContract? 如果是这样,什么是最佳做法?

I don't want to use the KnownType, because I shouldn't have to update the endpoint every time someone decides to inherit my DataContract. I can't write any DataContractResolver, because I didn't extend the exact structure of the DataTable class. Is it possible to to extend a DataContract from the client? If so, what's the best practice?

谢谢!

推荐答案

我不建议使用Datatable,这会使WCF易于在客户端和服务器序列化方面遇到问题,例如需要指定表名.最好使用自定义数据类型,我们可以将继承类型与KnownType属性一起使用.
https://docs. microsoft.com/zh-CN/dotnet/framework/wcf/feature-details/data-contract-known-types
在我这边,我不能使用继承的数据表,而可以通过使用Knowntype属性使用任意的自定义类.
请参考我的代码段.

I don't recommend using the Datatable, which makes it easy for WCF to have problems with client and server serialization, such as the need to specify a table name. It is best to use a custom data type, we can use the inheritance type with the KnownType attribute.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-known-types
On my side, I can't use the inherited Datatable, while I could use an arbitrary custom class by using Knowntype attribute.
Please refer to my code segments.

        [DataContract]
    [KnownType(typeof(Product))]
   public class MyData
    {
        [DataMember]
        public ProductBase Product { get; set; }
    }


    [DataContract]
    public class ProductBase
    {
        [DataMember]
        public int ID { get; set; }
    }
    [DataContract]
    public class Product : ProductBase
    {
        [DataMember]
        public string Name { get; set; }

    }

这篇关于传递继承的“数据合同";通过WCF调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆