I am developing a windows service which utilizes the following classes:
The service uses binary serialization to send instances of EnableAccountTask and AccountExpirationWarningTask to a remote object cache and XML serialization to commit instances of classes to a remote SQL 2005 database.
I have been using XML serialization with these classes for some time without any problems and have only just implemented binary serialization. The binary serialization implementation required the addition of the IPersistable interface (which did not exist previously) and the Serialization() attribute on each class. XML serialization worked perfectly prior to these additions.
After making these modifications, binary serialization works perfectly but XML serialization appears to be broken. When attempting to XML serialize an array of PersistableObject[] containing instances of AccountExpirationWarningTask and/or EnableAccountTask, the following exception is thrown:
[tt]System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type Task was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.[/tt]
As you can see from my code above, I am using XmlInclude. While debugging, I was able to verify (using array[0].GetType().BaseType.GetCustomAttributes()) that XmlInclude is an attribute of the Task class and the PersistableObject classes, and that the correct types are being included.
My XML serialization code is as follows:
The only possible cause I can think of is that the addition of IPersistable is causing problems, but it is a necessity as my caching components are in another project and I cannot create a circular dependency. I have not seen anything to indicate that Serializable() and XmlInclude() are mutually exclusive, so I don't see why that would be an issue.
The application performs binary serialization prior to XML serialization, however XML serialization still fails if I attempt to perform XML serialization first.
Does anyone have any ideas what might cause this problem? I'm quite stuck
Code:
interface IPersistable;
[Serializable()]
[XmlInclude(typeof(Task))]
abstract class PersistableObject : IPersistable;
[Serializable()]
[XmlInclude(typeof(AccountExpirationWarningTask))]
[XmlInclude(typeof(EnableAccountTask))]
abstract class Task : PersistableObject;
[Serializable()]
class EnableAccountTask : Task;
[Serializable()]
class AccountExpirationWarningTask : Task;
The service uses binary serialization to send instances of EnableAccountTask and AccountExpirationWarningTask to a remote object cache and XML serialization to commit instances of classes to a remote SQL 2005 database.
I have been using XML serialization with these classes for some time without any problems and have only just implemented binary serialization. The binary serialization implementation required the addition of the IPersistable interface (which did not exist previously) and the Serialization() attribute on each class. XML serialization worked perfectly prior to these additions.
After making these modifications, binary serialization works perfectly but XML serialization appears to be broken. When attempting to XML serialize an array of PersistableObject[] containing instances of AccountExpirationWarningTask and/or EnableAccountTask, the following exception is thrown:
[tt]System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type Task was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.[/tt]
As you can see from my code above, I am using XmlInclude. While debugging, I was able to verify (using array[0].GetType().BaseType.GetCustomAttributes()) that XmlInclude is an attribute of the Task class and the PersistableObject classes, and that the correct types are being included.
My XML serialization code is as follows:
Code:
private bool Commit(PersistableObject[] objects)
{
System.IO.MemoryStream stream =
new System.IO.MemoryStream();
System.Xml.Serialization.XmlRootAttribute xmlRoot =
new System.Xml.Serialization.XmlRootAttribute();
xmlRoot.ElementName = "ObjectList";
System.Xml.Serialization.XmlSerializer serializer =
new System.Xml.Serialization.XmlSerializer(typeof(Pers istableObject[]), xmlRoot);
serializer.Serialize(stream, objects);
...
}
The only possible cause I can think of is that the addition of IPersistable is causing problems, but it is a necessity as my caching components are in another project and I cannot create a circular dependency. I have not seen anything to indicate that Serializable() and XmlInclude() are mutually exclusive, so I don't see why that would be an issue.
The application performs binary serialization prior to XML serialization, however XML serialization still fails if I attempt to perform XML serialization first.
Does anyone have any ideas what might cause this problem? I'm quite stuck