WJM
WJM

Reputation: 1191

XML (de)serializing errors namespace

I have a generic serializer and deserializer for messages that are sent over a network connection:

 public static async Task<string> SerializeObject<T>(Object obj)
    {
        string objectStr;
        using (var memStream = new MemoryStream())
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(memStream, (T)obj);

            memStream.Position = 0;

            using (var sr = new StreamReader(memStream))
            {
                objectStr = await sr.ReadToEndAsync();
            }
        }
        return objectStr;
    }

    public static async Task<T> DeserializeObject<T>(string obj)
    {
        using (var memStream = new MemoryStream())
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(obj);
            memStream.Write(data, 0, data.Length);
            memStream.Position = 0;
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            T result = (T)(serializer.ReadObject(memStream));

            return result;
        }
    }

and I have a simple class that I serialize and deserialize. This is one of many classes like this, that's why I have the generic (de)serializer:

[DataContract]
public class SignatureMessage : NetworkMessage
{
    [DataMember]
    public string AppName { get; set; }
    [DataMember]
    public string AppUserName { get; set; }
    [DataMember]
    public string AppUserID { get; set; }
    [DataMember]
    public string IPAdress { get; set; }
    [DataMember]
    public string Port { get; set; }
}

When I set my project to 'Release' instead of 'Debug'. I get an XMLException EncounteredWithNameNamespaceExpectingElement. After research online, the suggestion was made to change my class to have an empty namespace in the datacontract element like this:

[DataContract(Namespace = "")]
public class SignatureMessage : NetworkMessage
{
    [DataMember]
    public string AppName { get; set; }
    [DataMember]
    public string AppUserName { get; set; }
    [DataMember]
    public string AppUserID { get; set; }
    [DataMember]
    public string IPAdress { get; set; }
    [DataMember]
    public string Port { get; set; }
}

When I tried this I got a different XMLException XmlPrefixBoundToNamespace. How can I apply the proper namespacing so both serializing and deserializing will work?

Upvotes: 2

Views: 144

Answers (2)

Manuel Zelenka
Manuel Zelenka

Reputation: 1634

At first there were two things which catched my eyes:

  1. You pass an Object to your SerializeObject method instead of the actual type T.
  2. The empty namespace. This empty namespace tip is given in an MSDN article for visual basic. But still I'd ignore this tip and set the namespace to something unique and meaningful. Just remember: the namespace has to be the same on the client and the service for the same type.

If these two things won't help your cause you could possibly be running into a KnownType-Issue. Therefore I'd also recommend reading this article and utilizing the GenericResolver used in it.

Edit: Although my tips might not help you with your problem they are still true. But I noticed something in the full exception that you posted. http://schemas.datacontract.org/2004/07/ is the default namespace for a DataContract if none was specified explicitly. It could mean that you are trying to send something from Listen.Extensions. So this may also be a good starting point to look for possible errors.

Upvotes: 1

Jeroen Heier
Jeroen Heier

Reputation: 3694

I tried to reduce the problem. I put all code in one console application, removed the async and await keywords, removed the NetworkMessage inheritance and used public static async Task<string> SerializeObject<T>(T obj). No problems in debug and release mode. So look at the NetworkMessage implementation (not specified, not a .Net class I know) and be sure to use the same dll (with SignatureMessage and the other classes) on client and server.

Upvotes: 0

Related Questions