2015-09-05 75 views
0

對於我而言,我無法弄清楚爲什麼Nest只在索引實例時序列化下面的基類的屬性,即使我告訴它索引派生類。索引派生的[ElasticType]只能序列化基類的屬性

的基類:

[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類的屬性。我已經試過:

  • 使派生類的公共
  • 添加[ElasticType]的派生類
  • 檢查鳥巢的源代碼(這是因爲源代碼是很複雜很困難的,而且的NuGet包我引用,即使它是最新的一個,似乎是最新的源代碼不向前兼容)

回答

0

顯然,基本的默認JSON序列不連載性質w^ith一個null值。在我的情況下,我的財產值是null,所以這就是爲什麼他們沒有被序列化。當值不是null時,序列化程序正常工作。