Unable to deserialize XML body with root name 'Binary' and root namespace?
Hi,
I am(client) trying to communicate with a webservice with ASP.NET WCF and have come along way to solve this(more information here : Connect to a password protected server with WCF over HTTP).
When running the request method :
var proxy = factory.CreateChannel();
var response = proxy.GetMyData(id);
I get the following exception :
Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService', 'http://tempuri.org/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.Serialization.SerializationException: Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService', 'http://tempuri.org/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.
Could this be a encoding problem? How do I solve it?
BestRegards
Edit1 : I have added the following line to the interface of the "service" :
[ServiceKnownType(typeof(MyObject))]
But this does not solve the problem.
I also want to add that a new exception have bin thrown now :
The data at the root level is invalid. Line 1, position 1. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.
This is often the first exception I get but when trying again I get the other exception?
The webservice is set up to return two types of XML documents, the real message contains some information and to deserilize this in my applicaiton I开发者_StackOverflow社区 have created the corsponding classes with XmlRoot and XmlElement attributes (this is the class that I have added as known typ to the service interface). The other message that can be returned will only contain a string and when this happen an exception will probably be thrown in my client becouse It is trying to deserlize data into a type that do not support it. Its possible that the first exception (Unable to deserialize XML body with root name 'Binary') is thrown when I get a propper response and the second exception ( The data at the root level is invalid. Line 1, position 1 ) is probably when I get the not supported message.
I thought that the attributes and the channelfactory should make it easy but I can´t see what XML I get back so Im now thinking of switching my solution to simple work with the XMLDocumet class. This is not a good solution but I will see exacly what´s happening.
Edit2
If I run the URL against the webservice on my own computer I will get the following reply :
<Error><code>E50</code><desc>IP Not Allowed</desc></Error>
Nothing more, and If I run it on the correct server(my host) the replay should according to the customer be something like this :
<Desc>
<Make cfe_code="98" cfe_value="Volkswagen" label="Märke" value="Volkswagen"/>
<ModelName cfe_code="99" cfe_value="Touareg" label="Modell" value="Touareg"/>
<BodyType cfe_code="212" cfe_value="22" label="Kaross" value="SUV"/>
...
</Desc>
Edit3:
I have now tried this code on the host server :
public XDocument GetMyData(string regNr)
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("MyName", "MyPassword");
Stream s = wc.OpenRead("http://MyServer.com/ag/get?UID=9999999999.eu_vddsall_xml&VINREG=" + regNr + "&LANG=sv");
// return an XDocument for LINQ
XmlTextReader xmlReader = new XmlTextReader(s);
XDocument xdoc = XDocument.Load(xmlReader);
xmlReader.Close();
xdoc.Save(HostingEnvironment.MapPath("~/App_Data/Files/Xml/") + DateTime.Now.ToString("hhmmssfff") + ".xml");
return xdoc;
}
And this works great! I get a XML file that contains the following :
<?xml version="1.0" encoding="utf-8"?>
<Desc>
<Vin cfe_code="11" cfe_value="X" label="Chassie nr" value="X" />
<Make cfe_code="98" cfe_value="Volvo" label="Märke" value="Volvo" />
<ModelName cfe_code="99" cfe_value="S70" label="Modell" value="S70" />
<BodyType cfe_code="212" cfe_value="1" label="Kaross" value="Sedan" />
...
</Desc>
So why does not my WCF version work?
Given your linked question, it seems like you're using the webHttpBinding
in your client factory. If this is the case, that binding internally uses the WebMessageEncodingBindingElement
, which defines a composite encoder to read messages from the wire. The encoder has three inner encoders, each one responsible for dealing with a specific content type:
- a "text" encoder, responsible for dealing with XML content (text/xml, application/xml, etc);
- a "JSON" encoder, responsible for dealing with JSON content (text/json, application/json, etc);
- a "binary" or "raw" encoder, responsible for dealing with all the other content types.
The binary encoder exposes a message with a body with a single element (called <Binary>
), and the actual HTTP body as base64-encoded data in the content. So what you likely have is a server which is not returning XML data (or at least not with a XML content-type), so the XML exposed by the encoder is not what the XmlSerializer
expects.
Check with a network tool such as Fiddler to see what the server is actually returning.
This is what I learned :
WCF is able to build REST services but WCF does not provide additional help for consumption of this service. Instead you should use WebClient or HttpWebRequest to communicate with the REST service. WCF does however provide a way of communicating with SOAP services else this would be vary hard to do.
So the Edit3 of my post is the correct way of communicating with a REST service.
In my case the xDocument will pars the output from the WebClient to a XDocument. Then I will manually have to convert this XDocument to my view classes (ASP.NET MVC)
精彩评论