locked
Sending message with XmlAnyElement declared data member to remote host fails RRS feed

  • Question

  • Hello,

    I've got two services on two hosts. The first one sends message to the second one using the following message type:
    public class Report : Update<ReportRequestType, PortSet<DefaultUpdateResponseType, Fault>>
        {
            public Report() { }
    
            public Report(ReportRequestType body) : base(body) { }
    
            public Report(ReportRequestType body, PortSet<DefaultUpdateResponseType, Fault> responsePort)
                : base(body, responsePort) { }
        }
    
        [DataContract]
        public class ReportRequestType
        {
            private object _report;
    
            public ReportRequestType() { }
    
            public ReportRequestType(object report)
            {
                _report = report;
            }
    
            [DataMember]
            [XmlAnyElement]
            public object Report
            {
                get { return _report; }
                set { _report = value; }
            }
        }
    As one can see the data member Report is unspecified and therefore declared with XmlAnyElement. The first service sets the value of this property to a simple data contract, which is cloneable. If both services are hosted in the same dss host, I get no problems. In the case of two hosts I get an error message:
    Cannot serialize member of type SimpleDataContract: XmlAnyElement can only be used with classes of type XmlNode or a type deriving from XmlNode.
    Thus I started to derive SimpleDataContract from XmlNode until I realized that this is not possible (only internal or private constructors available).

    Does there exists a way to solve this problem or do I have to use a concrete type for the property Report.

    Thx in advance.

    Jens
    Thursday, September 24, 2009 2:06 PM

Answers

  • On two hosts, I think the message has to be xml serialized in order to transfert it. The 'object' class is not xml serializable by default. If you want to use XmlAnyElement, the type of your "Report" property should be XmlNode. In human language the xml serializer says "Ok, if it could be any element, you have to do the serialization job for me because I can't guess how to serialize an object".

    So once you have changed the type of your Report property, you have to write the code to fill it. It will be something like :

    StringWriter sw = new StringWriter();
    XmlSerializer serializer = new XmlSerializer(report.GetType());
    serializer.Serialize(sw,report);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(sw.ToString());
    Request.Report = doc.DocumentElement;

    DSS provides also a serialization port that could do the job for you if your type is known by the DSS

    Regards,
    Vincent


    http://www.simplysim.net/
    • Marked as answer by Jens Sauer Tuesday, September 29, 2009 4:31 PM
    Friday, September 25, 2009 6:52 AM