次の WCF REST Web サービス インターフェイスがあります。
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(
UriTemplate = "foobar/",
Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare),
]
void PostFoobar(Foobar foobar);
}
実装:
public class Service : IService
{
public void PostFoobar(Foobar foobar)
{
try
{
Log.MonitoringLogger.Info("foo" + foobar.foo);
Log.MonitoringLogger.Info("bar" + foobar.bar);
}
catch (Exception ex)
{
if (Log.ExceptionLogger.IsErrorEnabled) Log.ExceptionLogger.Error(ex);
}
}
}
私のFoobarクラス:
[DataContract]
public class Foobar
{
[DataMember]
public string foo { get; set; }
[DataMember]
public string bar { get; set; }
}
しかし、クライアントから呼び出すと、パラメーターの Foobar オブジェクトは常に NULL のようです。次のメソッドを実装しようとしました:
void PostFoobar(String foo, String bar);
そして今回はうまくいきました!私の質問は次のとおりです。JSON Foobar オブジェクトを送信すると、デシリアライズされないのはなぜですか?
これは、Wireshark で作成したキャプチャで、クライアントが期待どおりの JSON オブジェクトを実際に送信したかどうかを確認するものです。
したがって、クライアントは期待どおりにオブジェクトを送信しているようです: { "foo": "foo text", "bar": "bar text" }