Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi ^_^

Just when I think my Workflow Engine is in order, this happens >_<<br mode="hold" />
So, I have an instance of an IMessage that I need to clone. here is that process:
C#
public IMessage Requeue()
{
    //New instance of this object.  Clone() is below
    var message = Clone();
    if (ReferenceEquals(message, this))
    {
        throw new InvalidOperationException("Clone must return a different object!");
    }

    //Reset the new instance
    message.Id = 0;
    message.Started = false;
    message.Complete = false;
    message.ReadTimestamp = null;
    message.CompleteTimestamp = null;
    message.Due = DateTime.Now + TimeSpan.FromSeconds(10);
        
    //save to the db    
    message.Save();

    //I only use this return for logging atm
    return message;
            
}

protected virtual IMessage Clone()
{
    IMessage message = (IMessage)Activator.CreateInstance(GetType());
    
    //Need to set the values of the derived type (methods below)
    message.SerializedData = Serialize();
    message = DeSerialize(message);

    return message;
}

public string Serialize()
{
    Type thisType = GetType();
    var serializer = new XmlSerializer(thisType);
    TextWriter writer = new StringWriter();
    serializer.Serialize(writer, this);

    //My issue is that in several cases, writer.ToString()==null?!?
    return writer.ToString();
}


The messages need to carry forward the derived type property values or the process is useless. I caught a case at message.Save(); where the SerializedData was null and reran the Clone() method. That time it worked.

My message parser is multi-threaded, but each message parse is handled in a single thread from start to finish (including re-queuing a clone).

I don't get why this isn't working unless XmlSerializer has some race condition issues or is not truly thread-safe. I couldn't find any evidence of this online, but I'm close to writing my own XmlSerializer. It seems like it would be easy enough but I don't want to if I don't have to.


Any advice would be appreciated
Thanks ^_^
Andy
Posted

1 solution

If you are using Serialization & Deserialization for cloning purposes then BinaryFormatter be be better:

C#
public static T DeepClone<t>(T obj)
{
 using (var ms = new MemoryStream())
 {
   var formatter = new BinaryFormatter();
   formatter.Serialize(ms, obj);
   ms.Position = 0;

   return (T) formatter.Deserialize(ms);
 }
}</t>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900