0

次の 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 オブジェクトを実際に送信したかどうかを確認するものです。 Wireshark キャプチャ

したがって、クライアントは期待どおりにオブジェクトを送信しているようです: { "foo": "foo text", "bar": "bar text" }

4

1 に答える 1

0

興味深いものを見つけました: chrome プラグインSimple REST clientで動作します。jsonデータを送信しました:

{ "foo": "foo text", "bar" : "bar text" }

そしてそれはうまくいきました。

そこで、IIS サーバーのログを分析したところ、違いが見つかりました。クライアントから要素がキャッチされません。次のオブジェクト (ログ内)

<root type="object" xmlns="">
  <foo type="string">foo chrome</foo>
  <bar type="string">bar chrome</bar>
</root>

Chrome クライアントからサービスを呼び出した場合にのみ表示されます。

于 2013-09-12T13:28:18.167 に答える