1

当我使用 POST 时,ServiceStack Json 客户端没有反序列化我的结果时遇到问题。get 方法将响应反序列化为 UserCredentials 对象没有问题,但是当我使用 POST 方法时,它返回一个具有所有 null 属性的对象。如果我将传递给 Post 方法的类型更改为字典,我可以看到它得到了正确的结果,并且我可以看到来自 Fiddler 的其余调用在 http 级别成功。

   public UserCredentials Login(string uname, string pwd)
    {
        var url = "/login";
        //note that I have to send a dictionary because if I sent a Login Object with username and password it erronously sends {Login:} in the raw request body instead of {"Uname":uname, "Password":password}

        var login = new Dictionary<string, string>() { { "Uname", uname }, { "Password", pwd } };
        var result = client.Post<UserCredentials>(url, login);

        return result;

    }

这是响应(这是来自服务器的正确和预期的 http 响应)

HTTP/1.1 200 OK
Server: nginx
Date: Thu, 14 Mar 2013 12:55:33 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Length: 49

{"Uid":1,"Hash":"SomeHash"}

这是 UserCredentials 类

  public class UserCredentials
  {
    public long Uid;
    public string Hash;
  }
4

2 回答 2

1

我认为您只需要制作 Uid 和 Hash 公共属性而不是公共字段。不过,如果是这种情况,我不确定您的 GET 请求如何正确反序列化。

public class UserCredentials
{
    public long Uid {get; set;}
    public string Hash {get; set;}
}
于 2013-03-14T21:47:50.097 回答
1

您的 ServiceStack 服务器端代码是什么样的?在服务器上执行以下操作应该允许您像这样使用 JsonServiceClient:

调用登录服务:

var client = new JsonServiceClient("http://url.com/login");
var response = client.Post<LoginResponse>(new Login() {Uname = "username", Password = "secret"});
var credentials = response.Credentials;

登录服务实现:

public class Login : IReturn<LoginResponse>
{
    public string Uname { get; set; }
    public string Pasword { get; set; }
}

public class UserCredentials
{
    public long Uid {get; set;}
    public string Hash {get; set;}
}

public class LoginResponse : IHasResponseStatus
{
    public LoginResponse()
    {
        this.ResponseStatus = new ResponseStatus();
    }

    public UserCredentials UserCredentials { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
}

public class LoginService : Service
{
    public LoginResponse Post(Login request)
    {
        //Code to Get UserCredentials
        return new LoginResponse {UserCredentials = new UserCredentials()};
    }
}
于 2013-03-14T17:41:04.003 回答