使用 servicestack MsgPack 客户端时无法反序列化服务响应

Unable to deserialize service response when using servicestack MsgPack client

在使用 servicestack MsgPackServiceClient 时尝试反序列化来自服务的响应时出现以下错误。

异常:{"Cannot deserialize member 'test1' of type 'System.Int32'."}

内部异常:{"Cannot convert 'System.Int32' type value from type 'FixedRaw'(0xA4) in offset 1."}

服务器端 Servicestack 服务:

    public class TestService : Service
    {
        public test Get(test s)
        {

            return new test { test1 = 12, test2 = "testvalue", Domian = "1234" };
        }
    }

服务器端 DTO:

[Route("/test")]
public class test
{
    public int test1 { get; set; }
    public string test2 { get; set; }
    public string Domain { get; set; }
}

客户端代码:

class Program
{
    static void Main(string[] args)
    {
        MsgPackServiceClient c = new MsgPackServiceClient(@"http://localhost:52862/");

        var result = c.Get<test>(@"/test");

    }
}

客户端dto:

public class test
{
    public int test1 { get; set; }
    public string test2 { get; set; }
}

客户端我们不需要域属性。当我们尝试获取值时,会抛出上述异常。

当我们添加域 属性 时它工作正常并且我们能够获取值。

我们真的需要拥有所有属性吗?

请帮我解决这个问题。谢谢你的时间。

如果您使用像 MsgPack 这样的二进制格式,您应该使用 exact DTO 用于序列化,许多二进制序列化器在设计时都希望如此。

如果您只想在客户端使用部分 DTO,您应该改用 JSON 等灵活的文本序列化程序。