NullReferenceException thrown during deserialization with Auditing

Posts   
 
    
ErinaG
User
Posts: 22
Joined: 31-Dec-2014
# Posted on: 08-Jan-2016 20:21:06   

Hi,

I'm setting up a project to use Auditing and I've run into an issue with XML deserialization. I've written the Auditors WriteXml/ReadXml methods based on this thread - https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=13609 - and they look like this:


public override void WriteXml(System.Xml.XmlWriter writer, SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect aspects, 
  System.Collections.Generic.Dictionary<System.Guid, SD.LLBLGen.Pro.ORMSupportClasses.IEntityCore> processedObjectIDs)
{
  foreach(EntityClasses.AuditEmployeeEntity auditInfoEntity in _auditInfoEntities)
  {
    auditInfoEntity.WriteXml(writer, SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect.Compact |
      SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect.DatesInXmlDataType |
      SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect.MLTextInCDataBlocks);
  }
}

public override void ReadXml(System.Xml.XmlNode auditorNode)
{
  System.Xml.XmlReader reader = new System.Xml.XmlNodeReader(auditorNode);
  this.ReadXml(reader);
}

public override void ReadXml(System.Xml.XmlReader reader)
{
  reader.MoveToContent();
  string startElementName = reader.LocalName;
  while (reader.Read() && !((reader.LocalName == startElementName) && (reader.NodeType == System.Xml.XmlNodeType.EndElement)))
  {
    EntityClasses.AuditEmployeeEntity toDeserialize = new EntityClasses.AuditEmployeeEntity();
    toDeserialize.ReadXml(reader);
    _auditInfoEntities.Add(toDeserialize);
  }
}

Now in my main application I have something like this...

Dim employeeXmlString As String = String.Empty
employeeGeneralEntity.WriteXml(SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect.Compact Or _
                            SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect.DatesInXmlDataType Or _
                            SD.LLBLGen.Pro.ORMSupportClasses.XmlFormatAspect.MLTextInCDataBlocks, employeeXmlString)
Session(StringConstantSession.ProfilePackageKey) = employeeXmlString

[...]

employeeGeneralEntity = New EntityClasses.EmpGeneralEntity
Dim profilePackage As String = Session(StringConstantSession.ProfilePackageKey).ToString()
employeeGeneralEntity.ReadXml(profilePackage)

The last line - ReadXml - throws this error

System.NullReferenceException was caught HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=SD.LLBLGen.Pro.ORMSupportClasses StackTrace: at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.Xml2Entity(XmlNode node, Dictionary2 processedObjectIDs, List1 nodeEntityReferences) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.2\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\EntityBase2.cs:line 1452 at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore1.ReadXml(XmlNode node, XmlFormatAspect format) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.2\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Core\EntityCore.cs:line 3061 at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore1.ReadXml(String xmlData) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v4.2\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Core\EntityCore.cs:line 3035 at [...].Page_Load(Object sender, EventArgs e) InnerException:

The XML being deserialized looks like this:

<EmpGeneralEntity Id="1">
  <Fields>
    [...]
  </Fields>
  <EntityState>Fetched</EntityState>
  <IsNew>false</IsNew>
  <IsDirty>true</IsDirty>
  <Auditor>
    <AuditEmployeeEntity Id="">
      <Fields>
        [...]
      </Fields>
      <EntityState>New</EntityState>
      <IsNew>true</IsNew>
      <IsDirty>true</IsDirty>
      <ObjectID>7b781eb7-ca2b-4e6c-9f82-92a24eeaee6b</ObjectID>
    </AuditEmployeeEntity>
  </Auditor>
  <ObjectID>83659f7e-c574-49e3-885e-8a9ac19f8dba</ObjectID>
</EmpGeneralEntity>

I'm using v4.2 The Auditors are added by overriding the CreateAuditor method with templates.
This deserialization worked before adding Auditing.
Any help would be appreciated.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Jan-2016 17:32:02   

Hi Erina,

Do you have a repro project we can use to test on our side?

Where are you overriding these methods? (on EmpGeneralEntity?, if so, What about the xml of the employee itself?)

David Elizondo | LLBLGen Support Team
ErinaG
User
Posts: 22
Joined: 31-Dec-2014
# Posted on: 11-Jan-2016 15:30:48   

I'm overriding these methods in my EmpGeneralEntityAuditor class, because I need to serialize my EmpGeneralEntity AND its Auditor.

From the documentation:

If an entity containing an Auditor is passed across tiers for example, by using WCF or Webservices, the data collected inside an auditor has to be serialized into XML and restored when the Auditor is deserialized. To be able to do so, you should override in your Auditor classes the WriteXml and one of the ReadXml methods.

I have done so, and serialization seems to work correctly, producing the XML given above. I've downloaded the LLBLGen Pro source code and the exact line throwing the error is this

EntityBase2.cs, Line 1453:

string auditorAssemblyName = currentElement.Attributes["Assembly"].Value;

It seems the code isn't checking if the Assembly attribute exists before reading its value. The very next line suggests that we shouldn't assume the Assembly attribute will always be there:

if(auditorAssemblyName == null)

Perhaps a check was overlooked?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 11-Jan-2016 17:19:22   

Please state the LLBLGen runtime library build number. Please refer to the forum guidelines for more details.

ErinaG
User
Posts: 22
Joined: 31-Dec-2014
# Posted on: 11-Jan-2016 17:41:09   

Runtime library 4.2.14.0903

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 12-Jan-2016 18:07:21   

Could you please download the latest v4.2 build from the customer area and try again? your runtime is outdated.

Frans Bouma | Lead developer LLBLGen Pro
ErinaG
User
Posts: 22
Joined: 31-Dec-2014
# Posted on: 12-Jan-2016 18:43:00   

Now using 4.2.15.1216

No change. Same error.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Jan-2016 06:46:30   

daelmo wrote:

Hi Erina,

Do you have a repro project we can use to test on our side?

David Elizondo | LLBLGen Support Team
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 13-Jan-2016 14:23:02   

I tried to repro it.

The XML generated has the Assembly attribute in the Auditor node. The call never goes to the Auditor's ReadXML method.

We are looking into it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 13-Jan-2016 16:17:47   

In the auditor you're using an xmlwriter, which uses Compact25 format by default. You're serializing the xml as Compact however. Compact25 doesn't serialize the assembly name into the xml and the formats aren't compatible. You have to use Compact25 in your application as well, which is recommended, Compact25 is more compact and much faster and the default.

So you should serialize your xml with compact25 format in your application, that should make the error go away at least (as it uses a different xml serializer pipeline).

Frans Bouma | Lead developer LLBLGen Pro
ErinaG
User
Posts: 22
Joined: 31-Dec-2014
# Posted on: 13-Jan-2016 16:34:07   

Thank you. Using Compact25 works. simple_smile