Error while reading body of request message - c#

I need to read content of message in WCF project like
var messageContent = Encoding.UTF8.GetString(OperationContext.Current.RequestContext.RequestMessage.GetBody<byte[]>());
But in result I got an error:
Expecting element 'base64Binary' from namespace
'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered
'Element' with name 'Human', namespace
'http://numans.hr-xml.org/2007-04-15'.
Can you please suggest me what Im doing wrong?
Content that I'm sending are:
<Human xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://numans.hr-xml.org/2007-04-15">
<HumanId>
<guid>c2ee9a7e-c7a8-48e0-910b-47c2012bfa8e</guid>
</HumanId>
...
</Human>
Also I tried to read content like:
var messageContent = OperationContext.Current.RequestContext.RequestMessage.ToString();
Result of messageContent:
...stream...

GetBody<T> is used to deserialize the message body as type T. So when you call GetBody<byte[]>(), the deserializer expects base64-encoded binary data but finds the <Human> element.
If you only want to read the message body as string, use GetReaderAtBodyContents which returns an XmlDictionaryReader, at which you can use ReadOuterXml().
If you want to read the body as typed content, create a Human class from its XML representation and use GetBody<Human>().

Related

How to include CDATA parameter into a C# soap call

I have a soap client and in order to make a call to a service of my company I need, among others, a parameter containing a CDATA string.
Simple version of the C# code I have is the following:
ServiceRef.GetArraySoapClient client = new ServiceRef.GetArraySoapClient();
String codes = #"
<Codes>
<Code><Batch>AAA</Batch><Item>YYY</Item></Code>
<Code><Batch>BBB</Batch><Item>XXX</Item></Code>
</Codes>";
client.GetArray("uname", "pword", "<![CDATA[" + codes + "]]>");
When I did the same using SoapUI, it works. But within the C# code, it gives me an error that goes like "error in the format of Code items".
I don't understand what is wrong with defining CData like this?
Okay, apparently I don't need to add something special before and after 'codes' while sending the request.

Asternet AGI GeneralMappingStrategy using XML

I am trying to use XML for my GeneralMappingStrategy in Asternet. I have my program working fine using List
such as:
agiServer.MappingStrategy = new GeneralMappingStrategy(
new List<ScriptMapping>()
{
new ScriptMapping() {
ScriptName = "testIVR",
ScriptClass = "Asterisk_Test.testIVR",
}
});
But I'd rather have it read an XML file as it says it can do in the documentation, however it does not seem to say anywhere what the XML format is required.
I have tried:
string pathtoxml = "test.xml";
agiServer.MappingStrategy = new GeneralMappingStrategy(pathtoxml);
With my XML as:
<?xml version="1.0"?>
<ScriptMapping>
<ScriptName>testIVR</ScriptName>
<ScriptClass>Asterisk_Test.testIVR</ScriptClass>
</ScriptMapping>
As a complete guess, seemed to make sense, but this won't compile, I get errors of:
System.InvalidOperationException: 'There was an error reflecting type 'System.Collections.Generic.List`1[AsterNET.FastAGI.MappingStrategies.ScriptMapping]'.'
Does anyone happen to know how to do this?
It appears that there was an issue with the Aster.NET library, I've now submitted the fix and it's been accepted. For anyone who has an issue on this in the future, the XML format is:
<?xml version="1.0"?>
<ArrayOfScriptMapping xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema">
<ScriptMapping>
<ScriptName>testIVR</ScriptName>
<ScriptClass>Asterisk_newTest.testIVR</ScriptClass>
</ScriptMapping>
</ArrayOfScriptMapping>

Error while deserializing an XML document using RestSharp

I am trying to deserialize the following XML response using RestSharp:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
<ns0:reason>token successfully created</ns0:reason>
<ns0:success>true</ns0:success>
<ns0:authDetails>
<ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
<ns0:expiresIn>604800</ns0:expiresIn>
<ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
</ns0:authDetails>
</ns0:content>
This is a snapshot of my code:
IRestResponse response = client.Execute(request);
RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer();
payload apiresponse = deserial.Deserialize<payload>(response);
And this is the error that I am getting:
An unhandled exception of type 'System.Xml.XmlException' occurred in
System.Xml.dll Additional information: Data at the root level is
invalid. Line 1, position 1.
any ideas what I am doing wrong?
Thanks for all the replies.
I did some more investigation and after printing the content of the response to a string, it turned out that RestSharp was actually converting it from XML to JSON. No idea why it was doing that (i certainly wasn't specifying it, perhaps it's a default setting).
So because the response was a JSON then the XML deserializing was obviously throwing an error!
Thanks again.
Well, the exception message is pretty clear: Line 1 has invalid syntax:
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
The XML should probably look like this instead:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:payload xmlns:ns0="http://www.website.co.za/JSON_Token">
<ns0:content>
<ns0:reason>token successfully created</ns0:reason>
<ns0:success>true</ns0:success>
<ns0:authDetails>
<ns0:accessToken>feefaee94822a92ca7f134f74588cc69081b0e94</ns0:accessToken>
<ns0:expiresIn>604800</ns0:expiresIn>
<ns0:refreshToken>bc036cba4d346bf76809e143879cb8fb6983940c</ns0:refreshToken>
</ns0:authDetails>
</ns0:content>
</ns0:payload>
If you cannot change how the XML response is generated, you should pre-process the XML using common string-manipulation since it is invalid XML and hence cannot be parsed using standard tools.

Unexpected Character when Parsing .NET-Encoded JSON with JavaScript

I'm developing an ASP.NET Web Pages app, and I'm seeing a problem where the Javascript JSON.parse() method is unable to parse JSON output by the .NET Json.Encode() method. My specific problem is with the ampersand (&) character (Unicode U+0026).
For example, executing this code:
object SomeObject = new { SomeProperty = "A&B" };
Response.Write(Json.Encode(SomeObject));
In my .cshtml file results in the following content in the response:
{"SomeProperty":"A\u0026B"}
Which leads to a SyntaxError: Unexpected token u in my JavaScript:
function SomeCallback(aRequest) {
if (aRequest.status === 200) {
var lResponseJSON = JSON.parse(aRequest.Response); // Error on this line
}
}
How can I get the .NET JSON encoding and the JS JSON decoding to play nice when special characters are involved?
(Preferably, short of manually going through the stringified JSON before it's parsed to replace the unicode encodings)
EDIT: Might be worth mentioning that using Json.Write(SomeObject, Response.Output) instead of Response.Write(Json.Encode(SomeObject)) has no effect on the JSON output.
Your problem has to be somewhat different that you are showing:
When I run this code through my console:
var k = JSON.parse('{"SomeProperty":"A\u0026B"}')
console.log(k);
// Object {SomeProperty: "A&B"}
everything behaves correctly.
Though it looks strange, this is valid JSON:
{"SomeProperty":"A\u0026B"}

Working With Web Response Stream and De-serialization into C# object

I have 2 specific questions with regards to passing a System.IO.Stream (from a method) and deserialization into object (another method).
XML Response I get from a WebRequest (please note there are no root tags)
<?xml version="1.0" encoding="UTF-8"?>
<response id="-2d953936:14174bb0cf3:-5213">
<date>2013-10-01 12:01:55.532999</date>
<status>
<current>open</current>
<next>after</next>
<change_at>16:00:00</change_at>
</status>
<message>Market is open</message>
<unixtime>1380643315</unixtime>
</response>
Method 1 - ResponseMethod - Currently returning string
private static string GetResponse(HttpWebRequest request)
{
var v_Response = request.GetResponse();
var v_DataStream = v_Response.GetResponseStream();
var v_Reader = new System.IO.StreamReader(v_DataStream);
var x_XMLResponse = v_Reader.ReadToEnd();
//Close all Stream logic
v_Reader.Close(); v_DataStream.Close(); v_Response.Close();
return x_XMLResponse;
}
Method 2 - Convert the XML to an object
// I would use XDocument and Lin2XML to get my typed object - for example MarketStatus
Questions are:
I am currently passing string from Method 1. That doesnt help me in deserializing from XML to object. Should I be passing the return value as StreamReader and then use that as an input into method 2 to get my typed object. Is that a standard approach or there are better ways to this?
My ultimate objective is that the return value from second method should be an object.
Additional Note:
The reason this functionality is broken into 2 methods because I want the web response method and deserailization separate for testing purposes.
I don't have an XSD but have created a MarketStatus Class
Any code snippets/suggestions will really appreciate
We typically use a generic method, similar to the following (simplified for posting), which uses the XMLSerializer to deserialize an XML string representation into the corresponding object.
public T ReturnObjectfromXml<T>(string xmlForm)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
StringReader sr = new StringReader(xmlForm);
XmlTextReader xts = new XmlTextReader(sr);
return ((T)xs.Deserialize(xts));
}
Hope this helps.
Regards,
It looks like you are doing some type of message passing. You will be better off using WCF or ASP.NET Web API instead of rolling your own infrastructure code (to serialize and de-serialize).
To answer question 1: No, it is better to return a string and dispose of the reader as soon as you are done with it. See When should I dispose my objects in .NET?
Comment on note 1: In most cases, you wouldn't want to unit test the serialization/deserialization.

Categories

Resources