示例#1
0
        public void OnSerialized(MessageSerializerEventArgs e)
        {
            var handler = Serialized;

            if (handler != null)
            {
                handler(this, e);
            }
        }
示例#2
0
        public virtual string Serialize(object message)
        {
            string serializedMessage;

            if (message != null)
            {
                if (message is string)
                {
                    serializedMessage = message.ToString();
                }
                else if (message is Exception)
                {
                    serializedMessage = ((Exception)message).ToXmlString();
                }
                else if (message is DataSet)
                {
                    serializedMessage = ((DataSet)message).GetXml();
                }
                //else if (message.GetType().Name.Contains("AnonymousType")
                //    || message.GetType().Name.EndsWith("Request")
                //    || message.GetType().Name.EndsWith("Response"))
                //{
                //    try
                //    {
                //        var dic = ToDictionary(message);
                //        if (dic.ContainsKey("Exception")
                //            && dic["Exception"] is Exception)
                //        {
                //            dic["Exception"] = ((Exception)dic["Exception"]).ToXmlString();
                //        }
                //        var element = CreateXElement("Message", dic);
                //        serializedMessage = element.ToString();
                //    }
                //    catch
                //    {
                //        serializedMessage = "Message was not serialize for type#" + message.GetType();
                //    }
                //}
                else
                {
                    try
                    {
                        var dic = ToDictionary(message);
                        if (dic.ContainsKey("Exception") &&
                            dic["Exception"] is Exception)
                        {
                            dic["Exception"] = ((Exception)dic["Exception"]).ToXmlString();
                        }
                        var element = CreateXElement("Message", dic);
                        serializedMessage = element.ToString();
                    }
                    catch
                    {
                        serializedMessage = "Message was not serialize for type#" + message.GetType();
                    }
                }
            }
            else
            {
                serializedMessage = "null";
            }

            if (string.IsNullOrEmpty(serializedMessage))
            {
                serializedMessage = "null";
            }

            serializedMessage = serializedMessage.StartsWith("<Message>")
                         ? serializedMessage
                         : string.Format("<Message>{0}</Message>", serializedMessage);

            var eventParameter = new MessageSerializerEventArgs(message, serializedMessage);

            OnSerialized(eventParameter);
            return(eventParameter.SerializedMessage);
        }