'Aftter conversion to .NET 6.0, question mark is added to text
We have an ASP.NET Web API project that gets data from Java service that lives in Web Sphere and is exposed as a WCF service. After conversion to .NET 6.0, the Sax parser on the server side fails due to a ?
added to the beginning of the text.
We are sending
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="REQUEST">
...
</Message>
With .NET 4.8 and .NET 5.0, we do not have any problem.
With .NET 6, on the server side, a ?
shows up:
?<?xml version="1.0" encoding="utf-8"?>
On the client, in the debugger or when I save the serialized string to a file the extra ?
is not present.
The entire string is created via deserialization of an instance of C# class and after calling Encoding.UTF8.GetString()
The only change is the upgrade to .NET 6.
Any suggestion is appreciated, thank you.
Update more details the message is contracted from an instance of c# class that has properties that some of them are classes too example you can see below
[XmlRoot(ElementName = "Message")]
public class RequestMessage
{
[XmlElement(ElementName = "Content")]
public RequestContent Content { get; set; }
...
}
Serialization is done by:
public static string Serialize(object obj)
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
using (MemoryStream outStream = new MemoryStream())
{
ser.Serialize(outStream, obj);
outStream.Flush();
byte[] buffer = outStream.GetBuffer();
return Encoding.UTF8.GetString(buffer, 0,
(int)outStream.Position);
}
}
Sending request via creating WCF channel and executing service method:
{
SecureMessageHandlerServiceChannel channelClient =
_wcfSecureMessageHandlerServicefactory.CreateChannel(new EndpointAddress(AuthenticatedHandlerServiceURL));
OperationContextScope scope = new OperationContextScope(channelClient);
ServiceRequestBody requestBody =
new ServiceRequestBody(secureMessageSerializationType.XML_DIFF_GRAM, serializedInput);
var serviceRequest = new ServiceRequest(requestBody);
ServiceResponse resp = channelClient.Service(req);
}
We consume WCF services via service references that were created manually, see picture below for list of files
Again none of it has changed only upgrade to .NET6.0
Solution 1:[1]
For posterity: After bootstrapping to bare bones the issue turns out to be
public static string Serialize(object obj)
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
using (MemoryStream outStream = new MemoryStream())
{
ser.Serialize(outStream, obj);
outStream.Flush();
byte[] buffer = outStream.GetBuffer();
return Encoding.UTF8.GetString(buffer, 0,
(int)outStream.Position);
}
}
I changed it to
public static string Serialize(object obj)
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings { Indent = true, IndentChars = " " };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
ser.Serialize(xmlWriter, obj);
}
return textWriter.ToString();
}
}
that solved the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tech Wizard |