Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WCF Post SOAP
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)
        {
            // Call BLL here
            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);
 
                //Read the response into an xml document
                var soapResonseXmlDocument = new XmlDocument();
                soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
 
                //return only the xml representing the response details (inner request)
                TextBox1.Text = soapResonseXmlDocument.InnerXml;
                //Response.Write(soapResonseXMLDocument.InnerXml);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
 
The code works fine when the XML data is a given below
<?xml version="1.0" encoding="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
<?xml version="1.0" encoding="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 ?
Posted 6-Apr-13 23:08pm
Comments
MohamedHassanAli at 9-Apr-13 12:59pm
   
Anybody there who could help me out with this one ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Maciej Los 280
2 BillWoodruff 240
3 Afzaal Ahmad Zeeshan 239
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100