I'm playing around with ServiceStack and doing baby steps trying to understand the technology.
I've got a very simple setup (complete solution is available for download ):
Article
data. I installed the nuget package for ServiceStack.Pluging.MsgPack
and added the reference and properly set the AppHost plugin, as shown in the main()
code below.
These are the data class, DTO and Service classes I use:
public class Article : IReturn<Article>
{
public string AR_REF { get; set; }
public string AR_DESIGN { get; set; }
public string AR_CODEBARRE { get; set; }
public string FA_CODEFAMILLE { get; set; }
public string CT_INTITULE { get; set; }
}
public class Articles : IReturn<List<Article>> { }
public class ArticleService : Service
{
public List<Article> Get(Articles request) {
return new List<Article>() {
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
};
}
}
All this does is allow a client to get a list of 16 dummy Articles
.
static void Main() {
var appHost = new AppHost();
appHost.Plugins.Add(new MsgPackFormat());
appHost.Init();
appHost.Start("http://localhost:8883");
// Fails when calling client.Get()
var client = new MsgPackServiceClient("http://localhost:8883");
List<Article> response = client.Get(new Articles());
appHost.Stop();
}
With AppHost defined as:
public class AppHost : AppHostHttpListenerBase
{
static readonly ConfigurationResourceManager AppSettings = new ConfigurationResourceManager();
public AppHost() : base("Test", typeof(Article).Assembly) { }
public override void Configure(Funq.Container container) { }
}
The call to client.Get(...)
fails with a SerializationException
saying:
The unpacker did not read any data yet. The unpacker might never read or underlying stream is empty.
The exception occurs when I use MsgPackServiceClient()
with more than 15 records.
Strangely enough, if I return 15 or less Articles
instances, it works.
If I use JsvServiceClient()
or JsonServiceClient()
instead, I have no problem returning thousands of records.
I really don't know if I'm doing something very wrong or if there is something else at play here.
I'm using VS2012 with ServiceStack 3.9.43 (including the MsgPack plugin) installed through nuget.
The problem occurs regardless of the compilation options (AnyCPU/x86). No code optimization enabled (every solution/project option is the default one).
You can download the complete solution and try it yourself.
So, I'm not exactly sure what the issue is. It seems to be specific to using the MsgPackServiceClient within the same application of the AppHost. If you make the request from the browser to something like http://localhost:8883/Articles?format=x-msgpack
it appears to work.
Writing a test (like one at the bottom) seems to confirm that the MessagePackSerializer is working properly.
Adding a response filter (below) that writes to OutputStream seems to correct your issue. I tried with 16 and 32 records in the list.
Hope this helps.
appHost.ResponseFilters.Add((httpReq, httpRes, dto) =>
{
if (httpReq.ResponseContentType == ContentType.MsgPack)
{
using (var ms = new MemoryStream())
{
var serializer = MessagePackSerializer.Create(dto.GetType());
serializer.PackTo(Packer.Create(ms), dto);
var bytes = ms.ToArray();
var listenerResponse = (HttpListenerResponse)httpRes.OriginalResponse;
listenerResponse.OutputStream.Write(bytes, 0, bytes.Length);
httpRes.EndServiceStackRequest();
}
}
});
MessagePackSerializer Test
[Test]
public void test()
{
var arts = new List<Article>() {
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
new Article() {AR_CODEBARRE = "987654", AR_REF = "1002.ARRGHHH", AR_DESIGN = "BLAH BLAH TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "50FAM"} ,
new Article() {AR_CODEBARRE = "123456", AR_REF = "515.VEROTEST", AR_DESIGN = "THIS IS A TEST", CT_INTITULE = "ACME", FA_CODEFAMILLE = "10FAM"} ,
};
var serializer = MessagePackSerializer.Create<List<Article>>();
var ms = new MemoryStream();
serializer.PackTo(Packer.Create(ms), arts);
ms.Position = 0;
try
{
var obj = serializer.Unpack(ms);
Assert.IsNotNull(obj);
}
catch (Exception ex)
{
throw ex;
}
ms.Close();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.