DTO changes to null after being transfered from the client to the server
I have a very strange problem: The instance of my DTO that is non-null on the client side is received as null
on the server side. Any ideas what could cause this? I don't even know where I should start looking...
Some code (this is exactly the code I am executing):
Client side:
public class AuthenticatedUser
{
private readonly UserDto _user;
// ...
public bool SetNewPassword(string oldPasswordHash, string newPasswordHash)
{
using (var userService = new UserServiceClient())
开发者_运维问答 {
if(_user == null)
throw new InvalidOperationException("Client side");
return userService.SetNewPassword(_user, oldPasswordHash,
newPasswordHash);
}
}
}
The class UserDto
has automatically been created when I added the service reference.
Server side:
public class UserService : IUserService
{
// ...
public bool SetNewPassword(UserDto userDto, string oldPasswordHash,
string newPasswordHash)
{
using (var unitOfWork = _unitOfWorkFactory.Start())
{
if (userDto == null)
throw new InvalidOperationException("Server side");
var user = unitOfWork.Repository.Get<User>()
.ByUserNameAndPasswordHash(userDto.UserName,
oldPasswordHash);
if (user == null)
return false;
user.PasswordHash = newPasswordHash;
return true;
}
}
}
I am getting the InvalidOperationException
with the text "Server side"...
UPDATE:
TheUserDto
class on the server side looks like this:
public class UserDto
{
public UserDto() {}
public UserDto(User domainObject)
{
Mapper.Map(domainObject, this);
}
public String AcademicDegree { get; set; }
public String EmailAddress { get; set; }
public String FirstName { get; set; }
public Gender Gender { get; set; }
public Boolean HasChangedPassword { get; set; }
public Int32 Id { get; set; }
public Boolean IsActive { get; set; }
public String LastName { get; set; }
public IEnumerable<MailboxDto> Mailboxes { get; set; }
public String PasswordHash { get; set; }
public String PronunciationOfName { get; set; }
public String Role { get; set; }
public IList<ScheduledGreetingDto> ScheduledGreetings { get; set; }
public LanguageDto SpeakerLanguage { get; set; }
public String Title { get; set; }
public String UserName { get; set; }
}
Interestingly the _user
field on the client side I try to transmit is an instance the client got from the server via a call to another service method, i.e. transmitting the UserDto
seems to work in general...
The most likely scenario is that the UserDto
objects are not the same - namespace has changed, the properties do not match. If you're using generated proxies, try Update Service Reference...
to make sure client and server are in sync.
You might want to use Fiddler to see what's being sent between the two applications. This will let you check whether it's a serialization issue (the client isn't correctly sending the data) and a de-serialization issue (the server isn't correctly receiving the data).
精彩评论