Passing XML string in the body of WCF REST service using WebInvoke
Asked Answered
S

4

5

I'm a newbie to WCF, REST etc. I'm trying to write a service and a client. I want to pass xml as string to the service and get some response back.

I am trying to pass the xml in the body to the POST method, but when I run my client, it just hangs.

It works fine when I change the service to accept the parameter as a part of the uri. (when I change UriTemplate from "getString" to "getString/{xmlString}" and pass a string parameter).

I'm pasting the code below.

Service

[ServiceContract]
public interface IXMLService
{
    [WebInvoke(Method = "POST", UriTemplate = "getString", BodyStyle=WebMessageBodyStyle.WrappedRequest, 
    RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]

    [OperationContract]
    string GetXml(string xmlstring);
}

// Implementaion Code

public class XMLService : IXMLService
{
    public string GetXml(string xmlstring)
    {
        return "got 1";
    } 
}    

Client

string xmlDoc1="<Name>";        
xmlDoc1 = "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(@"http://localhost:3518/XMLService/XMLService.svc/getstring");
request1.Method = "POST";
request1.ContentType = "application/xml";
byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);        
request1.GetRequestStream().Write(bytes, 0, bytes.Length); 

Stream resp = ((HttpWebResponse)request1.GetResponse()).GetResponseStream();
StreamReader rdr = new StreamReader(resp);
string response = rdr.ReadToEnd();

Could somebody please point out what's wrong in it?

Skill answered 6/6, 2011 at 14:0 Comment(2)
Will it change if you use XElement instead of string as parameter? Also set Content-Length of the request.Heartthrob
Thank you for your reply. I tried using XElement. no luck!!Skill
C
8

Change your operation contract to use an XElement and the BodyStyle of Bare

[WebInvoke(Method = "POST", 
    UriTemplate = "getString", 
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetXml(XElement xmlstring);

Additionally I suspect you client code should contain (note the first +=):

string xmlDoc1="<Name>";
xmlDoc1 += "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";
Cindy answered 6/6, 2011 at 15:25 Comment(0)
C
1

You still need to create a class:

public class Test
{

    public string xmlstring{ get; set; }

}

You can also use fiddler to check if the serialized XML could be passed as a parameter.

Cozen answered 5/6, 2012 at 12:19 Comment(0)
P
0

I believe the problem is that you're setting the BodyStyle to WrappedRequest which would require your incoming XML to be wrapped in a <GetXml> element in whatever namespace your service contract is defined in. If you set the BodyStyle to Bare and use XElement as @Ladislav Mmka suggested in the comment you should be good to go.

Palpebrate answered 6/6, 2011 at 15:15 Comment(0)
S
0

You need to wrap your XML string in a <string/> tag with the appropriate Microsoft XML serialization namespace. This question has been answered before here on SO but, I can't find it at the moment.

Strephon answered 6/6, 2011 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.