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.
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);
}
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.