Reputation: 9676
Hi I have a service-proxy with one method:
void SendRequest(MyMessage msg);
The MyMessage is defined as follows:
[MessageContract(IsWrapped=false)]
public class MyMessage{
[MessageBodyMember(Order=0)]
public XmlElement Body;
public MyMessage(XmlElement Body){
this.Body = Body;
}
}
Now, the problem is that when I send the request, the Body is wrapped in a tag like so:
<s:Body>
<Body>
<MyMessage>
<SomeData>Hello world</SomeData>
</MyMessage>
</Body>
</s:Body>
when what I really want is:
<s:Body>
<MyMessage>
<SomeData>Hello world</SomeData>
</MyMessage>
</s:Body>
Can someone please help? I'm starting to become desperate! :/
EDIT: The reason I want to send XmlElement is that the service will accept a various amount of XML-formats, and will do the xsd-validation and transformation on the server-side. This is only supposed to be sort of a wrapper.
There is also no way I can have the endpoint-server simply accept the "wrong" xml-structure, as I'm not in control of that.
Upvotes: 0
Views: 2818
Reputation: 9676
Alright, so it seems I had to totally abadon WCF for this to work.
What I did was to create two simple methods:
XDocument ConstructSoapEnvelope(string messageId, string action, XDocument body)
{
XDocument xd = new XDocument();
XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace a = "http://www.w3.org/2005/08/addressing";
XElement soapEnvelope = new XElement(s + "Envelope", new XAttribute(XNamespace.Xmlns + "s", s), new XAttribute(XNamespace.Xmlns + "a", a));
XElement header = new XElement(s + "Header");
XElement xmsgId = new XElement(a + "MessageID", "uuid:" + messageId);
XElement xaction = new XElement(a + "Action", action);
header.Add(xmsgId);
header.Add(xaction);
XElement soapBody = new XElement(s + "Body", body.Root);
soapEnvelope.Add(header);
soapEnvelope.Add(soapBody);
xd = new XDocument(soapEnvelope);
return xd;
}
string HttpSOAPRequest(XmlDocument doc, string add, string proxy, X509Certificate2Collection certs)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(add);
req.ClientCertificates = certs;
if (proxy != null) req.Proxy = new WebProxy("", true);
req.Headers.Add("SOAPAction", "\"\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
return r.ReadToEnd();
}
ConstructSoapEnvelope simply creates a soap-envelope with a header and a body (tailored for my needs with ws-addressing)
and the HttpSOAPRequest is a slightly modified version of the one found here: http://www.eggheadcafe.com/articles/20011103.asp
I had to modify it to accept client-certificates, in order for my SSL-communication to work..
Hope this helps someone else as well! :)
Upvotes: 2