Skip to content

Latest commit

 

History

History
98 lines (80 loc) · 3.85 KB

PX1063.md

File metadata and controls

98 lines (80 loc) · 3.85 KB

PX1063

This document describes the PX1063 diagnostic.

Summary

Code Short Description Type Code Fix
PX1063 The declaration of the exception class does not contain a serialization constructor. This will cause incorrect deserialization of the exception data and will lead to runtime errors in Acumatica ERP. Error Available

Diagnostic Description

Every class that that is derived from the System.Exception class (including PXException-derived classes) must implement the serialization constructor. If the serialization constructor is not defined, its absense will lead to the loss of exception data during serialization or deserialization and can lead to runtime errors, especially when Acumatica ERP is used in cluster mode.

The code fix generates the serialization constructor for the exception class. The generated constructor will be private for sealed exception classes and protected for all other classes. The generated constructor will call the base serialization constructor. If the exception class declares new serializable data then the constructor will contain a call to the RestoreObjectProps static method of the PX.Common.ReflectionSerializer class. This class is responsible for Acumatica reflection-based serialization and deserialization of data. If the exception class does not declare new serializable data, the serialization constructor body will be empty.

This diagnostic considers data to be serializable if it is new data declared in the exception class that will be serialized by .Net serializers and Acumatica reflection-based serializers which are the following:

  • New instance fields if they are not marked with System.NonSerializedAttribute attribute
  • New auto-generated instance properties if they are not marked with the System.NonSerializedAttribute attribute

For example, the following cases are not considered serializable:

  • Static fields and constants
  • Static properties
  • Calculated properties
  • Properties with an explicit backing field, as shown in the following example
    public int Property 
    {
       get =>  _fieldFromBaseClass;
       set 
       {
    	   _fieldFromBaseClass = value;
       }
    }
  • Fields and properties that are marked as non serializable as shown in the following example
    [field: NonSerialized]
    public int Property { get; set; } 

Note: In older versions of Acumatica ERP, there is no PX.Common.ReflectionSerializer class because it was added later during refactoring. The PX.Common.PXReflectionSerializer class will be used instead; the names of the methods are the same.

Example of Incorrect Code for a Class Without Serializable Data

public class DocumentNotApprovedException : PXException
{	
	[field: NonSerialized]
	public Guid? DocumentNoteID { get; set; } 
}

Example of the Code Fix for a Class Without Serializable Data

public class DocumentNotApprovedException : PXException
{
	[field: NonSerialized]
	public Guid? DocumentNoteID { get; set; } 

	protected DocumentNotApprovedException(SerializationInfo info, StreamingContext context) : base(info, context)
	{
	}
}

Example of Incorrect Code for a Class with Serializable Data

public class CustomerNotFoundException : PXException
 {
	public int? CustomerID
	{
		get;
		set;
        }
}

Example of the Code Fix for a Class with Serializable Data

public class CustomerNotFoundException : PXException
 {
	public int? CustomerID
	{
		get;
		set;
        }

 	protected CustomerNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
	{
		ReflectionSerializer.RestoreObjectProps(this, info);
	}
}