Wcf Client: Passing XML string in the WCF REST service using WebInvoke

Go To StackoverFlow.com

2

With out parameter for Display method it is working in browser i.e http://localhost:2617/UserService.svc/test

When i add one parameter i am unable to browse it also.

I have the following contract.

[ServiceContract]
public interface IUserService
{
    [OperationContract]
    [WebInvoke(Method="PUT",UriTemplate = "/tes/{name}",   
    BodyStyle=WebMessageBodyStyle.WrappedRequest)]
    string Display(string name);
}

public string Display(string name)
{
        return "Hello, your test data is ready"+name;
}

I am trying to call using the following code

         string url = "http://localhost:2617/UserService.svc/test"; //newuser
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        string xmlDoc1 = "<Display xmlns=\"\"><name>shiva</name></Display>";
        req.Method = "POST";
        req.ContentType = "application/xml";
        byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
        req.GetRequestStream().Write(bytes, 0, bytes.Length); 

        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        Stream responseStream = response.GetResponseStream();
        var streamReader = new StreamReader(responseStream);

        var soapResonseXmlDocument = new XmlDocument();
        soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());

I am unable to get output for that.please help me on this.

2012-04-04 06:09
by shiva reddy
Your method on the client end is "POST" but on the server end you have Method="PUT" - I'd have thought they would have to be the same - try changing your server to POST perhaps - kmp 2012-04-04 07:20
i changed it POST also... i have tried in different ways but it is not working. - shiva reddy 2012-04-04 11:46
you havent declared a namespace, so the namespace would be http://tempuri.org - rather than blank - Chris 2012-04-05 07:30
Is there any reason on why you want the name to be a query string. I think when you are performing a POST/PUT you cannot have query string parameters - Rajesh 2012-04-05 15:56


1

There are a few things that are not quite right in your code.

Client

On the client you need to specify the namespace to be tempuri, since you have not declared an explicit one, so your client code would need to be this:

string url = "http://localhost:2617/UserService.svc/test"; //newuser
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string xmlDoc1 = "<Display xmlns=\"http://tempuri.org/\"><name>shiva</name></Display>";
req.Method = "POST";
req.ContentType = "application/xml";
byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
req.GetRequestStream().Write(bytes, 0, bytes.Length);

HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream responseStream = response.GetResponseStream();
var streamReader = new StreamReader(responseStream);

var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());

Service

On the service the UriTemplate is not quite right - you are specifying /tes/{name} so that will be expecting a URL like http://localhost:2617/UserService.svc/tes/shiva but you are wanting to post XML data to it in the body so you should change that to UriTemplate = "/test" (I am assuming you meant test and not tes as in your question).

Also, the method should be POST if you are wanting to POST data to it (the client needs to match the service and I am assuming what you have on the client is what you want).

So, in conclusion, your IUserService should look like this:

[ServiceContract]
public interface IUserService
{        
    [OperationContract]
    [WebInvoke(Method = "POST",
               UriTemplate = "/test",
               BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string Display(string name);
}
2012-04-05 13:07
by kmp


1

You still need to create a class

public class Test
{

    public string name { get; set; }

}

You can also use fiddler to check if {name:999} could be passed as a parameter.

2012-06-05 12:03
by iamtonyzhou
Ads