我在新版本的ServiceStack中返回自定义AutenticateResponse时遇到问题。此代码在以前版本的ServiceStack中有效,但在升级后,它不再按预期运行。
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时,我遇到了同样的问题。
答案 0 :(得分:1)
问题很可能是DataContract属性现在已被继承,如果DTO被标记为[DataContract]
,则选择加入only the properties marked with DataMember are serialized。
由于AuthenticateResponse是DataContract,如果要重新使用DTO,则应标记要使用[DataMember]
属性序列化的属性,例如:
[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; }
}