Reputation: 21
I need to write a REST service that accepts XML documents from a client application. I don't have access to the client application and it cannot be changed.
It sends documents using a HTTP POST with a content type of text/xml; charset="UTF-8".
I have tried two different Operation Contracts and they both have different issues...
First my Host code:
private static WebServiceHost _host;
public static void ConnectToHost()
{
string url = ConfigHelper.GetValue("WebService.config", "WebServiceURL");
Uri baseAddress = new Uri(url);
Type instanceType = typeof(CXMLService);
_host = new WebServiceHost(instanceType, baseAddress);
Type contractType = typeof(ICXMLService);
ServiceEndpoint endpoint = _host.AddServiceEndpoint(contractType, new WebHttpBinding(), "Web");
endpoint.Behaviors.Add(new WebHttpBehavior());
_host.Open();
}
If I use this...
[OperationContract]
[WebInvoke(UriTemplate = "SendText")]
Stream SendText(Stream s);
I can receive XML files using a content type of "text/plain" but if I switch it to "text/xml" which is what the client will be sending I get a 400 Bad Request.
If I use this...
[OperationContract]
[WebInvoke(UriTemplate = "SendXML", Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
XElement SendXML(XElement xml);
Then it works with "text/xml" but fails with a 400 Bad Request because the XML has a DOCTYPE element outside of the root. I cannot change this XML file. Here is a sample of the file...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.024/cXML.dtd">
<cXML payloadID="[email protected]"
timestamp="2000-10-12T18:39:09-08:00" xml:lang="en-US">
<Header>
/// data here
</Header>
<Request deploymentMode="test">
// data here
</Request>
</cXML>
Upvotes: 1
Views: 2838
Reputation: 31
Here is a basic way to stream an xml document to WCF service.
Contract:
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "ProfileRequest")]
Stream ProfileRequest(Stream value);
Service:
public Stream ProfileRequest(Stream value)
{
StreamReader reader = new StreamReader(value);
string text = reader.ReadToEnd();
XDocument post = XDocument.Parse(text);
XDocument response = ProfileRequest(post);
return new MemoryStream(Encoding.UTF8.GetBytes(response.ToString()));
}
Test Console:
string filePath = "C:\someFile.xml";
XDocument testDoc = XDocument.Load(filePath);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filePath);
string newDoc = xDoc.InnerXml.ToString();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(testDoc.ToString());
string localProfile = "http://localhost/WcfService/Service1.svc/ProfileRequest";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(localProfile);
webrequest.Method = "POST";
webrequest.ContentType = "text/xml";
webrequest.ContentLength = data.Length;
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
string strResult = string.Empty;
Encoding enc = System.Text.Encoding.GetEncoding("UTF-8");
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
strResult = loResponseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
Console.Write(strResult);
Upvotes: 3