AlexandreG
AlexandreG

Reputation: 1683

JsonServiceClient returns reponse with correct attribute but empty value

I am trying to make a JSON request on an external service, that would look like this :

GET request :

https://remotehost/path/mycount?foo=C&bar=21

response :

{"count":1000}

for this, I use ServiceStack JsonServiceClient, here is my code below

var client = new JsonServiceClient(classifiedSearchBaseURL);
var response = client.Get<CountResponse>(new MyRequest {
    foo = "C",
    bar = 21
});

class MyRequest
{    
    public string foo { get; set; }
    public int bar { get; set; }
}

class CountResponse
{
    public string count;
}

The problem is that I get a response object with an attribute "count" that is null, even though the server answers a 200 with a correct value/response (using Fiddler)

I tried changing the type of count from string to int but then I get 0 instead of null.

Any of you guys have an idea?

Thanks for your help!

Upvotes: 4

Views: 340

Answers (1)

mythz
mythz

Reputation: 143399

ServiceStack only serializes public properties by default so you need to change your Response DTO to:

class CountResponse
{
    public string count { get; set; }
}

I'd also recommend adding the IReturn<T> marker interfaces like:

class MyRequest : IReturn<CountResponse>
{    
    public string foo { get; set; }
    public int bar { get; set; }
}

So the Response Type can be automatically inferred by the client so your call sites can be reduced to:

var response = client.Get(new MyRequest { foo = "C", bar = 21 });

Upvotes: 3

Related Questions