即使使用 KnownType,派生类型的属性也不会在 WCF 客户端中反序列化
我有以下类型:
public enum MyEnum
{
Value1,
Value2
}
[DataContract]
public class Configuration
{
[DataMember]
public MyEnum MyValue { get; set; }
[DataMember]
public Credentials CredentialValues { get; set; }
}
[DataContract, KnownType(typeof(CustomCredentials))]
public class Credentials
{
}
[DataContract]
public class CustomCredentials : Credentials
{
[DataMember]
public string Property1 { get; set; }
[DataMember]
public string Property2 { get; set; }
}
在我的服务接口上,我有一个函数返回 Configuration
实例,并将其 CredentialValues
属性设置为完全填充的 实例自定义凭据
。我没有收到来自客户端或服务器的错误,但是当数据在服务器上进行属性序列化并由客户端接收时,CustomCredentials
上的属性永远不会有值。为了在客户端上正确反序列化这些属性,我需要在此处更改什么?
作为参考,客户端和服务器之间的连接是通过 NetTcpBinding
使用 DuplexChannelFactory
使用由客户端和服务应用程序共享的数据/服务契约项目(服务是自托管的),因此不存在需要重新生成的服务代理类型。
I have the following types:
public enum MyEnum
{
Value1,
Value2
}
[DataContract]
public class Configuration
{
[DataMember]
public MyEnum MyValue { get; set; }
[DataMember]
public Credentials CredentialValues { get; set; }
}
[DataContract, KnownType(typeof(CustomCredentials))]
public class Credentials
{
}
[DataContract]
public class CustomCredentials : Credentials
{
[DataMember]
public string Property1 { get; set; }
[DataMember]
public string Property2 { get; set; }
}
And on my service interface, I have a function that returns an instance of Configuration
with its CredentialValues
property set to a fully populated instance of CustomCredentials
. I receive no errors from the client or the server, but while the data is being property serialized on the server and received by the client, the properties on CustomCredentials
never have a value. What do I need to change here in order to have these properties properly deserialized on the client?
For reference, the connection between client and server is made with a DuplexChannelFactory
over a NetTcpBinding
using a data/service contract project that is shared by the client and service applications (the service is self-hosted), so there are no service proxy types that could need to be regenerated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此代码与您的 DataContracts 一起添加到 Contracts 项目中。
创建了服务。
创建了客户端。
按预期输出以下内容:
因此,这实际上在不修改任何数据契约的情况下起作用...如果我恢复到双向操作并直接返回
Configuration
而不使用回调,结果相同。还尝试使凭据抽象,但无法复制您的问题。
我错过了什么吗?
Added this code to the Contracts project along with your DataContracts.
Created the service.
Created the client.
Outputs the following as expected:
So this actually worked without modifying any of your data contracts... The same results if I revert to a twoway operation and just return
Configuration
directly without using the callback.Also tried making Credentials abstract but could not replicate your problem.
Have I missed something?