ServiceStack 3 ベースのクライアント サーバー アーキテクチャを使用しています。リクエスト DTO に抽象型のプロパティが含まれ、それを実装する 2 つの異なる具象クラスを持つサービスを作成しようとしています。抽象型は、抽象クラスまたはインターフェースのいずれかです。ただし、どちらの場合も、サーバーはプロパティで null オブジェクトを受け取ります。
3 つのアセンブリと対応する名前空間があります: TestClient
、Server
、およびCommonLib
クライアントとサーバーの両方によって参照されます。
つまり、次の 3 つのアセンブリに分散されます。
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
タイプに変更すると、これは機能します。インターフェイスのままにするか、抽象クラスに変更すると、データは として送信されます。This
IThisOrThat
IThisOrThat
null
これはシリアル化の問題だと思います。インターフェースを抽象クラスに変更し、適切な属性で装飾しようとしましたKnownType
が、ServiceStack のシリアライザーはこの恩恵を受けていないようです。これを行うためのトリックはありますか?