System.IO.Stream to Data Transfer Object prior to WCF REST Serialization?

后端 未结 1 1561
深忆病人
深忆病人 2021-01-26 12:49

This question builds upon an earlier question (although off-topic) I asked yesterday. Please give it a read first.

OK - in my WCF REST project, I have been converting m

相关标签:
1条回答
  • 2021-01-26 13:18

    I might be mis-understanding things here, but what you're trying to do is backwards. You would first get your domain object, then convert to the DTO, then write the serialized DTO to the stream:

    public virtual Stream GetGroupById(string id)
    {
        var user = UserRepository.GetUserById(id);
        var dto = new Contracts.UserDto
                  {
                      Email = user.Email.ToString(),
                      Password = user.Password.ToString(),
                      UserId = user.UserId.ToString(),
                      UserName = user.UserName.ToString()
                  }
    
        var serializer = new JavaScriptSerializer()l
        var bytes = Encoding.UTF8.GetBytes(serializer.Serialize(dto));
    
        return new MemoryStream(bytes);
    }
    

    The Stream being returned to the client will contain the JSON for the serialized DTO.

    0 讨论(0)
提交回复
热议问题