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.

4
  • 1
    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? Commented Apr 4, 2012 at 7:20
  • i changed it POST also... i have tried in different ways but it is not working.. Commented Apr 4, 2012 at 11:46
  • 1
    you havent declared a namespace, so the namespace would be tempuri.org - rather than blank. Commented Apr 5, 2012 at 7:30
  • 1
    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. Commented Apr 5, 2012 at 15:56

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.