Can we use Non-Serializable Classes or Types in an Orchestration ?
Biztalk is entirely Stateless. This is achieved by using Persistence Points. Incase of any failure during the processing of an Orchestration (for eg: power failure or Server Restart etc) BizTalk Server will continue to restart the Orchestration from the Last Persistence Point.
Details about the Persistence points will be Stored in BizTalk SQL Server DataBase by serializing all the data.
Hence Non-Serializable Classes or Types cannot be used directly in Orchestrations.
Don’t worry, we can use them in orchestration with an Atomic Scope.
Because persistence does not happen within an Atomic scope. The persistence point is created only when the scope completes its execution as it’s an All-or-Nothing scope.So, we can use Non – Serializable Classes or Types inside Atomic Scopes in an Orchestration.
Consider the below Sample:
1. Create a new BizTalk Project and add any dummy Schema.
2. Create a new Orchestration and add a receive shape to receive the message of the Type Schema1 which is created in Step1.
4. Now add an Expression shape to the orchestration and add code to extract a node from the incoming message and assign that to the variable you created in step3.
5. Try to build the solution. You end up with an error as shown below.
Error 1 a non-serializable object type ‘System.Xml.XmlNode xmlnode’ can only be declared within an atomic scope or service.
6. To resolve this, make the following changes to the solution.
i. Click on the white space of the Orchestration and make the Transaction Type of the Orchestration to Long Running.
ii. Add a Scope Shape and set the Transaction Type of the Scope to Atomic and move the Expression shape inside the Atomic Scope.
iii. Move the xmlnode Variable inside the Atomic Scope.
7. Now build the solution. It should work without any errors.
This is just an example. Although this approach is useful, this should be avoided for performance reasons. Alternatives such as XPaths should be considered.
Hope it Helps.