我有一个基于 ServiceStack 3 的客户端-服务器架构。我正在尝试创建一个服务,其请求 DTO 包含一个具有抽象类型的属性,两个不同的具体类实现它。抽象类型可以是抽象类也可以是接口;但是,无论哪种情况,服务器都会在属性中接收一个空对象。
客户端和服务器都引用了三个程序集和相应的命名空间:TestClient
、Server
和。CommonLib
也就是说,分布在三个程序集中:
namespace CommonLib.Services
{
public class GetThing : IReturn<GetThingResponse> // request DTO
{
public IThisOrThat Context { get; set; }
}
public class GetThingResponse
{
public Dictionary<int, string> Result { get; private set; }
public GetThingResponse(Dictionary<int, string> result) // response DTO
{
Result = result;
}
}
}
namespace CommonLib
{
public interface IThisOrThat { }
public class This : IThisOrThat { } // and so forth
}
namespace Server.Services
{
public class GetThing Service : IService
{
public object Get(GetThing request)
{
var foo = request.Context; // this is null
}
}
}
namespace TestClient
{
class Program
{
public const string WSURL = "http://localhost:61435/";
static void Main(string[] args)
{
using (var client = new JsonServiceClient(WSURL))
{
var result = client.Get(new GetThing
{
Context = new CommonLib.This("context info")
});
}
}
如果我将Context
属性更改GetThing
为 typeThis
而不是IThisOrThat
,则此方法有效。将其保留为接口,或更改IThisOrThat
为抽象类,导致数据以null
.
我假设这是一个序列化问题。我尝试将接口更改为抽象类并使用适当KnownType
的属性对其进行装饰,但 ServiceStack 的序列化程序似乎并没有从中受益。有什么诀窍可以做到这一点吗?