我正在玩弄 ServiceStack 并尝试了解这项技术。
我有一个非常简单的设置(完整的解决方案可供下载):
- 使用 ServiceStack 的独立 AppHost(自托管)
- 我启动服务器
- 然后我查询
Article
数据列表。
我安装了 nuget 包ServiceStack.Pluging.MsgPack
并添加了引用并正确设置了 AppHost 插件,如下面的main()
代码所示。
DTO 和服务类
这些是我使用的数据类、DTO 和服务类:
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"} ,
};
}
}
所有这一切都是允许客户端获得 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();
}
AppHost 定义为:
public class AppHost : AppHostHttpListenerBase
{
static readonly ConfigurationResourceManager AppSettings = new ConfigurationResourceManager();
public AppHost() : base("Test", typeof(Article).Assembly) { }
public override void Configure(Funq.Container container) { }
}
怎么了
调用client.Get(...)
失败并带有SerializationException
一句话:
解包器尚未读取任何数据。解包器可能永远不会读取或底层流为空。
当我使用
MsgPackServiceClient()
超过 15 条记录时会发生异常。
奇怪的是,如果我返回 15 个或更少的Articles
实例,它就可以工作。如果我使用
JsvServiceClient()
orJsonServiceClient()
代替,我可以毫无问题地返回数千条记录。
我真的不知道我是否做错了什么,或者这里是否还有其他事情在起作用。
附加信息和下载
我正在使用 VS2012 和通过 nuget 安装的 ServiceStack 3.9.43(包括 MsgPack 插件)。
无论编译选项如何 (AnyCPU/x86),都会出现此问题。未启用代码优化(每个解决方案/项目选项都是默认选项)。
您可以下载完整的解决方案并自己尝试。