对于我的生活,我无法弄清楚为什么Nest只是序列化以下基类'索引实例时的属性,即使我告诉它索引派生类。
基类:
[ElasticType(Name = "activity")]
public class Activity
{
[ElasticProperty(Name = "name")]
public string Name { get; set; }
[ElasticProperty(OptOut = true)]
public DateTimeOffset Timestamp
{
get { return DateTimeOffset.ParseExact(TimestampAsString, "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture).ToUniversalTime(); }
set { TimestampAsString = value.ToString("yyyyMMddTHHmmssZ"); }
}
[Obsolete("Use Timestamp, instead.")]
[ElasticProperty(Name = "timestamp")]
public string TimestampAsString { get; set; }
[ElasticProperty(Name = "application")]
public string Application { get; set; }
[ElasticProperty(Name = "application_version")]
public string ApplicationVersion { get; set; }
[ElasticProperty(OptOut = true)]
public IPAddress RemoteIpAddress
{
get { return IPAddress.Parse(RemoteIpAddressAsString); }
set { RemoteIpAddressAsString = value.ToString(); }
}
[Obsolete("Use RemoteIpAddress, instead.")]
[ElasticProperty(Name = "remote_ip_address")]
public string RemoteIpAddressAsString { get; set; }
}
派生类:
private class SearchCountsRetrievedActivity : Activity
{
[ElasticProperty(OptOut = true)]
public PunctuationlessGuid? PrincipalIdentityId
{
set { PrincipalIdentityIdAsString = value; }
}
[ElasticProperty(Name = "principal_identity_id")]
public string PrincipalIdentityIdAsString { get; set; }
}
我的索引包装器方法:
public Task IndexActivityAsync<TActivity>(TActivity activity)
where TActivity : Activity
{
return _client.IndexAsync(activity);
}
无论我做什么,通过网络发送的序列化JSON只包括Activity
类&#39;属性。我尝试了什么:
[ElasticType]
添加到派生类答案 0 :(得分:0)
显然,底层的默认JSON序列化程序不序列化具有null
值的属性。就我而言,我的属性值为null
,这就是为什么它们没有被序列化。当值不是null
时,序列化程序可以正常工作。