Search code examples
c#wcfposturiwebinvoke

How to load data from HTTPPOST request in server side?


I have a WebInvoke method like this;

[OperationContract]

    [WebInvoke(

        Method = "POST",
        UriTemplate = "/go",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml

    )]       
    string go(string name);

and I post the data like this;

System.Net.WebClient client = new System.Net.WebClient();

        string reply = client.UploadString(
            "http://localhost/HelloService/Service.svc/go",
            "POST",
            aString);

The question is how can I take the data from posted message in go() method without using a uri template like this;

UriTemplate = "/go({name})"

Because I want to sent large amount of data and I cannot sent it in uri template


Solution

  • Here is the solution;

    [OperationContract]
    [WebInvoke(
    BodyStyle = WebMessageBodyStyle.Wrapped,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    Method = "POST",
    UriTemplate="/go"
    )]  
    string MyWebServiceMethod(string name);
    

    and HTTP POST request is;

    POST /go Modify HTTPS/1.1
    Content-Type: application/json; charset=utf-8
    Host: WebserviceURL
    Content-Length: length
    
    {"name":"someName"}