2

私は2つのメソッドを使用して以下のようにRESTfulサービスを構築しています(注:ASPNETCompatilibilityModeをtrueに設定しています):

[WebInvoke]
string TestMethodA()
{
        string test = HttpContext.Current.Request.Form["xml"];
}

[WebInvoke]
string TestMethodB(string res)
{
        string xml = res;
}

ここで、MethodAにパラメーターを渡すためにクライアントを構築するとき、次のようにします。

request.AddParameter("xmlString", HttpUtility.HtmlEncode(requestBody));

そして、MethodBにメッセージを送信するには、次のようにします。

request.AddParameter("text/xml",requestBody, ParameterType.RequestBody);

今の質問は:

クライアントはパラメータを渡す方法をどのように知っていますか?クライアントはサーバーの実装を認識していません。

リクエストを送信するクライアントは、RestSharpApiを使用しています。

4

1 に答える 1

0

Since MethodB() take a string, WCF has no idea what it should look like. It could be XML, JSON, free text, whatever. In your implementation, you'll just have to document how to format the request and give that to whoever is implementing the client.

A better approach would be to create a C# object, mark it up with appropriate serialization attributes, and use that as the parameter to MethodB(). For example:

[DataContract]
public class MyDataContract{

    [DataMember]
    public string SomeString{get;set;}

    [DataMember]
    public int SomeNumber{get;set;}    
}

public void MethodB(MyDataContract arguments){
  //do stuff

}

This will allow the WCF infrastructure to automatically parse the arguments. You can also have WCF autogenerate help documentation from this.

于 2012-03-16T02:12:49.430 に答える