2014-01-22 50 views
1

我在ServiceStack的新版本中返回自定義AutenticateResponse的問題。此代碼在以前版本的ServiceStack中工作,但在升級之後,它不再按預期運行。在新版ServiceStack中返回自定義AuthenticateResponse的意外結果

的AuthenticateResponse

public class CustomAuthResponse : AuthenticateResponse 
{ 
    public List<CustomCompanyDTO> Companies { get; set; } 
    public List<string> Roles { get; set; } 
    public List<string> Permissions { get; set; } 
    public string DisplayName { get; set; } 
    public string Email { get; set; } 
} 

的服務

public class CurrentUserService : AppServiceBase 
{ 
    public object Any(CurrentUser cu) 
    { 
     CustomAuthResponse response = new CustomAuthResponse(); 

     response.DisplayName = UserSession.DisplayName; 
     response.Email = UserSession.Email; 
     response.Companies = UserSession.Companies; 
     response.UserName = UserSession.UserName; 
     response.Roles = UserSession.Roles; 
     response.Permissions = UserSession.Permissions; 
     return response; 
    } 
} 

在v3中我可以調用CurrentUserService並預期返回的所有數據。在v4中,當我調用CurrentUserService時,沒有任何自定義字段包含在響應中。

我可以解決此特定呼叫通過改變代碼如下:按預期工作

public class CurrentUserService : AppServiceBase 
{ 
    public object Any(CurrentUser cu) 
    { 
     CustomAuthResponse response = new CustomAuthResponse(); 
     var x = new 
     { 
      DisplayName = UserSession.DisplayName, 
      Email = UserSession.Email, 
      Companies = UserSession.Companies, 
      UserName = UserSession.UserName, 
      Roles = UserSession.Roles, 
      Permissions = UserSession.Permissions, 
     }; 
     return x; 
    } 
} 

上面的代碼。我當然可以改變我的代碼以這種方式工作,我主要想知道改變了什麼,因爲我很好奇它是否會影響我在其他地方的代碼。嘗試從Authenticate調用我的自定義CredentialsAuthProvider返回ny CustomAuthResponse時,我看到相同的問題。

回答

1

問題很可能是DataContract屬性現在被繼承,並且如果DTO被標記爲[DataContract]那麼它是選擇加入並且only the properties marked with DataMember are serialized

由於AuthenticateResponse是DataContract,如果你想重新使用DTO,你應該標記屬性你想序列化與[DataMember]屬性,e.g:

[DataContract] 
public class CustomAuthResponse : AuthenticateResponse 
{ 
    [DataMember] 
    public List<CustomCompanyDTO> Companies { get; set; } 
    [DataMember] 
    public List<string> Roles { get; set; } 
    [DataMember] 
    public List<string> Permissions { get; set; } 
    [DataMember] 
    public string DisplayName { get; set; } 
    [DataMember] 
    public string Email { get; set; } 
}