none
communicationException is caught: There was an error in serializing body of message ..: 'There was an error generating the XML document RRS feed

  • Question

  • Hi,

    I am getting this exception : communicationException is caught: There was an error in serializing body of message ..: 'There was an error generating the XML document. When I am trying to call a web service(third party) from my application (VS 2010) and my application environment is Windows Server 2008 64 bits.

    Recently we migrated our .Net code(c#,vb.net) from VS 2008(Windows server 2003 32 bits) to VS 2010(Windows server 2008 64 bits) and framework is the same .Net3.5.

    The application is working fine in WS 2003 environment but failing in WS 2008 environment and I am including entire inner and stacktrace exception here for better understanding.

    ==============================================================================

       

    {"There was an error generating the XML document."}

    at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterRequestHandlerProcessSoap.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterRequestHandlerProcessSoap.Write7_DrssRequest(String n, String ns, DrssRequest o, Boolean isNullable, Boolean needType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterRequestHandlerProcessSoap.Write9_DRSSAttachmentRequest(String n, String ns, DRSSAttachmentRequest o, Boolean isNullable, Boolean needType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterRequestHandlerProcessSoap.Write15_renderDocument(Object[] p)
       at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer3.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)

    ====================== ======================================================

     This is the inner xml i am passing to the service in XmlDocumnet object

    ==================

    <NewDataSet><Table><REP_DATE>19-Dec-2013 04:26:51</REP_DATE><Header><DCFID>535820</DCFID><DCFStatus>New</DCFStatus><ECGID>1580004</ECGID><AttentionTo>y1</AttentionTo><SiteFax>1-1-1</SiteFax><HasMultipleVisitError>N</HasMultipleVisitError></Header><StudyDetails><SponsorName>Alnylam Pharmaceuticals</SponsorName><StudyID>YProt4.0_T1</StudyID><SiteID>ys1</SiteID><Invstigator>Inv1</Invstigator><ScreeningNo>sub1</ScreeningNo><SubjectInitial>*</SubjectInitial><Gender>Male</Gender><VisitID>V10</VisitID><ReminderNo>1</ReminderNo><ReRaisedNo /><DateOfBirth>01-Jan-1947</DateOfBirth><IssuedDateTime>12-Dec-2013, 08:11:46</IssuedDateTime><DispatchDateTime>18-Dec-2013, 05:38:11</DispatchDateTime></StudyDetails><Custom_Label><lbl_SponsorName>New SPONSOR:</lbl_SponsorName><lbl_StudyID>New Protocol Number:</lbl_StudyID><lbl_SiteID>New Project Site ID:</lbl_SiteID><lbl_Invstigator>New Investigator Name:</lbl_Invstigator><lbl_ScreeningNo>New Subject ID:</lbl_ScreeningNo><lbl_SubjectInitial>New Subject Initials:</lbl_SubjectInitial><lbl_Gender>Test Gender 258 trial 2:</lbl_Gender><lbl_DateOfBirth>DATE OF BIRTH MODIFIED 258:</lbl_DateOfBirth><lbl_VisitID>New Visit ID:</lbl_VisitID><lbl_IssuedDateTime>ISSUED DATE:</lbl_IssuedDateTime><lbl_DispatchDateTime>DISPATCH DATE:</lbl_DispatchDateTime><lbl_ReminderNo>REMINDER NO.:</lbl_ReminderNo><lbl_ReRaisedNo>RE-RAISED NO.:</lbl_ReRaisedNo><lbl_DCFID>DCF NO.:</lbl_DCFID><lbl_DCFStatus>DCF Status:</lbl_DCFStatus><lbl_ECGID>ECG ID:</lbl_ECGID><lbl_ECGDateTime>New ECG Acquisition Date and Time:</lbl_ECGDateTime><lbl_AttentionTo>ATTENTION TO:</lbl_AttentionTo><lbl_SiteFax>PROJECT SITE FAX:</lbl_SiteFax><lbl_HasMultipleVisitError>Multiple ECG: Please tick the ECG to be processed</lbl_HasMultipleVisitError></Custom_Label><ECGSourceInformation><StudyID>YProt4.0_T1</StudyID><SiteID>ys1</SiteID><VisitID>V10</VisitID><SubjectID>sub1</SubjectID><RandomNumber>01</RandomNumber><SubjectInitial>*</SubjectInitial><Gender>Male</Gender><IsManualECG>Y</IsManualECG><IsRecDateMandatory>Y</IsRecDateMandatory><IsRecTimeMandatory>Y</IsRecTimeMandatory><AcqDateTime>12-Dec-2013, 18:40:01</AcqDateTime><DateOfBirth>01-Jan-1947</DateOfBirth></ECGSourceInformation><Custom_Label_ECG_Source><lbl_StudyID>Protocol Number:</lbl_StudyID><lbl_SiteID>Project Site ID:</lbl_SiteID><lbl_VisitID>Visit ID:</lbl_VisitID><lbl_ScreeningNo>Subject ID:</lbl_ScreeningNo><lbl_RandomNumber>Randomization No.:</lbl_RandomNumber><lbl_AcqDateTime>Acquisition Date Time:</lbl_AcqDateTime><lbl_SubjectInitial>Subject Initial:</lbl_SubjectInitial><lbl_Gender>Gender:</lbl_Gender><lbl_DateOfBirth>DOB:</lbl_DateOfBirth></Custom_Label_ECG_Source><QueryList><DRow><QueryID>Q_4448656</QueryID><QueryText>Please confirm correct ECG RECORDING DATE TIME.</QueryText></DRow><DRow><QueryID>Q_4448657</QueryID><QueryText>Please confirm correct ECG RECORDING DATE TIME.</QueryText></DRow></QueryList><FAXINFO><First>000-800001-6190</First><Second>000-117 Followed by 866-373-4072</Second><Third>+1-919-323-3710</Third></FAXINFO></Table></NewDataSet>

    =================

    Please help, thanks in advance..i hope the above information is enough to get a understating of the issue. Let me know if any other information is required to resolve this.

    Thanks,

    Rajesh Kumar

    India

    Monday, December 23, 2013 5:47 AM

Answers

  • I have found the solution of the above issue, sharing this someone might need this..

    The root cause was, .Net generates some codes( properties, in the Reference.cs)  when we add any service reference. 

    So by default the below code was generated by .Net in Reference.cs

     

    [System.Xml.Serialization.XmlElementAttribute(Order=2)]

            public object Payload {

                get {

                    return this.payloadField;

                }

                set {

                    this.payloadField = value;

                    this.RaisePropertyChanged("Payload");

                }

            }

     

    And I just changed the type of the property(below) from Object to XmlDocument  which now works fine and my issue got resolved.

     

    [System.Xml.Serialization.XmlElementAttribute(Order=2)]          public System.Xml.XmlDocument Payload          {              get {                  return this.payloadField;              }              set {                  this.payloadField = value;                  this.RaisePropertyChanged("Payload");              }          }

     

    Hope it will help others.

    Wednesday, January 29, 2014 7:32 AM

All replies

  •  

    Please help, thanks in advance..i hope the above information is enough to get a understating of the issue. Let me know if any other information is required to resolve this.

    Hi,

    Looks like the you are not handling the exception/Fault in your code, if you want to ensure that you are serialising the error correctly, then please try to use the following:
    #WCF error handling:
    http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx .

    Hope this helps.

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 24, 2013 6:36 AM
    Moderator
  • Hi Amy,

    Even if I handle the above exception but it still giving me the same error, I am suspecting any it is happening because of environmental changes ..earlier we were using windows server 2003 32 bits and now we have migrated our code on Windows sever 2008 64 bits.

    The above xml we are passing as payload to WS.

    The code i am including here for better understanding,

    //reportContent  is having xml data as string.

              XmlDocument xmlReportDocument = new XmlDocument();

                xmlReportDocument.LoadXml(reportContent);

          

                   RequestHandlerProcessSoapClient ws = new RequestHandlerProcessSoapClient();

                    ws.ClientCredentials.UserName.UserName = _reportFormatData.User;

                    ws.ClientCredentials.UserName.Password = _reportFormatData.Password;

         ///Create the request

                        myRequest request = new myRequest();

                        request.Requestor = new Requestor();

                        request.Requestor.ApplicationID = _reportFormatData.ApplicationId;

                        request.Requestor.Stylesheet = template;

                        request.Requestor.Environment = _reportFormatData.Environment;

                        request.Payload = xmlReportDocument;

                        ///Setup the attachements

                        myAttachmentRequest attachmentrequest = new myAttachmentRequest();

                        attachmentrequest.setRequest = request;

    myResponse serResp = ws.renderDocument(attachmentrequest); // Here we are getting that exception.

    Tuesday, December 24, 2013 8:10 AM
  • Hi RajeshBangaloreIndia,

    RequestHandlerProcessSoapClient is a custom class and renderDocument is a method within the class. renderDocument must have some sort of code to transform the response. Since you have migrated your environment from Windows Server 2003 32 bits to Windows Server 2008 64 bits, I would request you to check the versions of the installed dlls Microsoft.Xml

    There might be chance of installing incorrect version of the dlls which is causing this issue.

    Thanks


    Thanks Subhendu DE - DotNet Artisan If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful

    Tuesday, December 24, 2013 1:16 PM
  • The version I am using is System.Xml - v4.0.30319(C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll) which system sets it by default  and i tried with the other versions(old) too but the same issue..Even tried with the other framework(current 3.5, 4.0 too) but the same issue..I guess this could be known issue for Microsoft..  

    I’m guessing that what’s happened here is that something that was valid in an old version of .NET(VS2010) is no longer valid in the newer versions(may be on 64 bits system). Microsoft made some fairly hefty changes to the service client stuff when they built WCF.

    I tried with XElement(Linq) too but no luck there also.

    Thursday, December 26, 2013 7:15 AM
  • I guess there is one connect bug

    http://connect.microsoft.com/VisualStudio/feedback/details/523253/system-invalidoperationexception-the-type-xyz-may-not-be-used-in-this-context#

    Thursday, December 26, 2013 8:30 AM
  • Are you targeting your migrated code to run under .net 3.5 frameworks? If yes,

    Have you set your project->property->TargetFramework to 3.5?

    If it Web app? Have you set your AppPool to use .net 3.5 frameworks? As refer here

    I have successfully upgraded couple of web service consuming code from Server 2003 to Server 2008 and not encountered any such issues.

    Here are couples of clues.

    1. Check the .Net SP level of your old infrastructure and the new one; possibly you need to apply .net 3.5 SP1 for windows server 2008 R2.
    2. What type of service are you consuming? Try to delete all service reference and add a fresh Web Reference for .asmx or service reference for WCF than try it. Please note that, both approach uses different serialization technology
    3. Seems you have a 32 bit version of .net framework 4.0 is installed (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll)), you can install 64 bit of Framework and try it.
    4. Also try to configure WCF trace and see if you the tracing information open-up any other clue for this.

    Lingaraj Mishra

    Thursday, December 26, 2013 2:01 PM
  • Hi Lingaraj,

    Here is the answer of your queries...

    Are you targeting your migrated code to run under .net 3.5 frameworks? If yes, --Yes, before migration also it  was 3.5

    Have you set your project->property->TargetFramework to 3.5? --Yes

    If it Web app? Have you set your AppPool to use .net 3.5 frameworks? As refer here ,No we are accessing it from Windows application as a Service reference.

    1. I have already installed 3.5 with SP1 on WS 2008.

    2. It is a Service reference and already deleted all and added again.

    3. I am compiling my code using all the modes that is x86/x64/Any mode(Build mode) but in all modes it is giving the error.

    4. Even tried with fresh console application but it is giving the same error.

    5. Need to work on your last point- WCF Trace. 

    One thing to mention here is ..the inner error message is 

    [System.InvalidOperationException] = {"The type System.Xml.XmlDocument may not be used in this context."}

    Message = "The type System.Xml.XmlDocument may not be used in this context."

    Also tried with XElement (linq) but still the same.


    Monday, December 30, 2013 7:20 AM
  • I have found the solution of the above issue, sharing this someone might need this..

    The root cause was, .Net generates some codes( properties, in the Reference.cs)  when we add any service reference. 

    So by default the below code was generated by .Net in Reference.cs

     

    [System.Xml.Serialization.XmlElementAttribute(Order=2)]

            public object Payload {

                get {

                    return this.payloadField;

                }

                set {

                    this.payloadField = value;

                    this.RaisePropertyChanged("Payload");

                }

            }

     

    And I just changed the type of the property(below) from Object to XmlDocument  which now works fine and my issue got resolved.

     

    [System.Xml.Serialization.XmlElementAttribute(Order=2)]          public System.Xml.XmlDocument Payload          {              get {                  return this.payloadField;              }              set {                  this.payloadField = value;                  this.RaisePropertyChanged("Payload");              }          }

     

    Hope it will help others.

    Wednesday, January 29, 2014 7:32 AM