Hi,
I have a small WCF application. It uses POST method. The problem is the code is working fine as long as the XML is not enclosed inside a SOAP ENVELOPE.
This is the interface code
using System.ServiceModel;
using System.ServiceModel.Web;
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "auth")]
ResponseData Auth(RequestData rData);
}
}
The interface implementation is
namespace RestService
{
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData(string id)
{
return "You requested product " + id;
}
public ResponseData Auth(RequestData rData)
{
var data = rData.details.Split('|');
var response = new ResponseData
{
Name = data[0],
Age = data[1],
Exp = data[2],
Technology = data[3]
};
return response;
}
#endregion
}
}
The RequestData code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace RestService
{
[DataContract(Namespace = "http://www.eysnap.com/mPlayer")]
public class RequestData
{
[DataMember]
public string details { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace RestService
{
[DataContract]
public class ResponseData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Age { get; set; }
[DataMember]
public string Exp { get; set; }
[DataMember]
public string Technology { get; set; }
}
}
The client is a web application with a button which invokes the service. Code below
using System;
using System.IO;
using System.Net;
using System.Web.UI;
using System.Xml;
namespace WebClient
{
public partial class _default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
const string url = "http://localhost:35798/RestServiceImpl.svc/auth";
req = (HttpWebRequest) WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/xml; charset=utf-8";
req.Timeout = 30000;
req.Headers.Add("SOAPAction", "auth");
var xmlDoc = new XmlDocument {XmlResolver = null};
xmlDoc.Load(Server.MapPath("PostData.xml"));
string sXml = xmlDoc.InnerXml;
req.ContentLength = sXml.Length;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write(sXml);
sw.Close();
res = (HttpWebResponse) req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
TextBox1.Text = soapResonseXmlDocument.InnerXml;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
The code works fine when the XML data is a given below
="1.0"="utf-8"
<RequestData xmlns="http://www.eysnap.com/mPlayer">
<details>Ashu|29|7 Years|.NET</details>
</RequestData>
but when the xml data is enclosed within a soap envelope i get BAD request exception
="1.0"="utf-8"
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">
<soap:Body>
<RequestData xmlns="http://www.eysnap.com/mPlayer">
<details>Ashu|29|7 Years|.NET</details>
</RequestData>
</soap:Body>
</soap:Envelope>
Can anybody let me know i am doing anything wrong here ?