I am using ServiceStack 5.10.3 and I have two services defined as below
[Tag("Crypto")]
[Route("/crypto/transaction", "GET")]
public class QueryCryptoTx : IGet, IReturn<QueryCryptoTxResponse>
{
}
[Tag("Crypto")]
[Route("/crypto/transaction", "GET")]
public class GetCryptoTx : IGet, IReturn<GetCryptoTxResponse>
{
public int Id { get; set; }
}
public class QueryCryptoTxResponse : IHasResponseStatus
{
public List<CryptoTx> Results { get; set; } = new ();
public ResponseStatus? ResponseStatus { get; set; } = null;
}
public class GetCryptoTxResponse : IHasResponseStatus
{
public CryptoTx Result { get; set; } = new ();
public ResponseStatus? ResponseStatus { get; set; } = null;
}
Both services access an external service, using my implementation of a ServiceStack Service Gateway, such that both services end up in the Send method below:
public T Send<T>(IReturn<T> request, string method, bool sendRequestBody = true, string? idempotencyKey = null)
{
var relativeUrl = request.ToUrl(method);
var body = sendRequestBody ? request.GetDto().ToJson() : null;
var json = Send(relativeUrl, method, body, idempotencyKey);
if (json.StartsWith("["))
{
json = "{" + json + "}"; // would fail is not wrapped with { }
}
var response = json.FromJson<T>();
return response;
}
The service returning a single value (GetCryptoTx) works without issue; either populating the Result or the ResponseStatus, as expected.
The service returning a list of values (QueryCryptoTx) populates the ResponseStatus when there is an error. But, when there is no error, it is not populating the Results property:
{
“results”: ,
“responseStatus”: null
}
Incidentally, if I do not wrap the json with “{” and “}”, the json.FromJson() line, fails with a complaint that the json does not start with “{”.
{
"results": [],
"responseStatus": {
"errorCode": "SerializationException",
"message": "Type definitions should start with a '{', expecting serialized type 'QueryCryptoTxResponse', got string starting with: [{\"id\":1234,\"transactionOwner\":\"PRIVATE",
"stackTrace": "[QueryCryptoTx: 2021-01-17 17:20:52]:\n[REQUEST: {}]\r\nSystem.Runtime.Serialization.SerializationException: Type definitions should start with a '{', expecting serialized type 'QueryCryptoTxResponse', got string starting with: [{\"id\":1234,\"transactionOwner\":\"PRIVATE\r\n at ServiceStack.Text.Common.DeserializeTypeRefJson.StringToType(ReadOnlySpan`1 strType, TypeConfig typeConfig, EmptyCtorDelegate ctorFn, KeyValuePair`2[] typeAccessors) in C:\\BuildAgent\\work\\912418dcce86a188\\src\\ServiceStack.Text\\Common\\DeserializeTypeRefJson.cs:line 172\r\n at ServiceStack.Text.Common.DeserializeType`1.StringToTypeContext.DeserializeJson(ReadOnlySpan`1 value) in C:\\BuildAgent\\work\\912418dcce86a188\\src\\ServiceStack.Text\\Common\\DeserializeType.cs:line 58\r\n at ServiceStack.Text.Json.JsonReader`1.Parse(ReadOnlySpan`1 value) in C:\\BuildAgent\\work\\912418dcce86a188\\src\\ServiceStack.Text\\Json\\JsonReader.Generic.cs:line 110\r\n at ServiceStack.Text.Json.JsonReader`1.Parse(String value) in C:\\BuildAgent\\work\\912418dcce86a188\\src\\ServiceStack.Text\\Json\\JsonReader.Generic.cs:line 83\r\n at ServiceStack.Text.JsonSerializer.DeserializeFromString[T](String value) in C:\\BuildAgent\\work\\912418dcce86a188\\src\\ServiceStack.Text\\JsonSerializer.cs:line 47\r\n at ServiceStack.StringExtensions.FromJson[T](String json) in C:\\BuildAgent\\work\\912418dcce86a188\\src\\ServiceStack.Text\\StringExtensions.cs:line 553\r\n at ADG.ServiceInterface.TreasuryGateway.Send[T](IReturn`1 request, String method, Boolean sendRequestBody, String idempotencyKey) in C:\\Yoyo\\Alchemy\\ADG\\code\\backend\\ADG.ServiceInterface\\Treasury\\TreasuryGateway.cs:line 102\r\n at ADG.ServiceInterface.TreasuryGateway.Send[T](IReturn`1 request) in C:\\Yoyo\\Alchemy\\ADG\\code\\backend\\ADG.ServiceInterface\\Treasury\\TreasuryGateway.cs:line 137\r\n at ADG.ServiceInterface.TreasuryServices.Get(QueryCryptoTx request) in C:\\Yoyo\\Alchemy\\ADG\\code\\backend\\ADG.ServiceInterface\\Treasury\\TreasuryServices.cs:line 76\r\n at ServiceStack.Host.ServiceRunner`1.ExecuteAsync(IRequest req, Object instance, TRequest requestDto) in C:\\BuildAgent\\work\\3481147c480f4a2f\\src\\ServiceStack\\Host\\ServiceRunner.cs:line 134\r\n",
"errors": [],
"meta": null
}
}
I am sure I have made mistakes, but could somebody explain:
-
Why is the Results property not being populated from deserializing the Json array?
-
Can I configure the Json deserializer to accept an array, so I do not need to wrap it as an object with “{” and “}”?