Compensation (BizTalk Server Sample)
The Compensation sample demonstrates how to use the Compensate shape in an orchestration.
This sample demonstrates how to compensate the transaction that has already been committed in the orchestration using the following sequence of steps:
Enter the customer data in the InfoPath form and submit the data to an orchestration that has been exposed as a Web service.
The orchestration has two parallel actions. One returns an acknowledgment to the InfoPath form and the other updates the Northwind and BTSCompensationSampleMailingList databases.
In the second action, the orchestration maps the incoming message to a customer relationship management (CRM) application message format and then updates the Customers table in the Northwind database. After this, the Mailing List table in the BTSCompensationSampleMailingList database is updated.
If either update fails, the changes made to the Northwind database are compensated by calling an external assembly to write the original customer data back to the database.
A Scope shape is primarily used for transactional execution and exception handling, including compensation. A scope consists of two blocks. The first block is the context block and the second block can be one or more exception-handling or compensation blocks. This is similar to the try-catch statement in the .NET programming language. In the UpdateContact.odx orchestration, there are two parallel actions. At the right-side branch, the try block is the Scope shape named Updated Backend Systems, which has a Long-Running transaction type and an Upd_Backend transaction identifier. Further down in the flow, there is a catch block that catches the general exception and initiates the compensation.
The orchestration must itself be a long-running transaction for you to set the transaction type to Atomic or Long-Running.
In the try block, there are two scopes named Update CRM and Update Mailing. Both of these scopes are atomic transactions. In the Update CRM scope, there is a try block and a compensation block. The try block updates the Northwind database through a method call to an external assembly. The compensation block is where the compensation action takes place. When an exception happens in the Update Backend Systems scope, the code in Undo CRM Expression Shape updates the record back to its original state. This is triggered by the Compensate shape in the Catch Exception block.
Atomic transactions guarantee that any partial updates are rolled back automatically in the event of a failure during the transactional update, and that the effects of the transaction are erased (except for the effects of any .NET calls that are made in the transaction).
A long-running transaction is considered committed when the last statement in it has completed. There is no automatic rollback of state in case of a transaction abort. You can achieve this programmatically through the exception and compensation handlers demonstrated in this sample.
In the catch block, there is one Delay shape set for ten seconds. This delays the compensation action. There is also a Compensate shape that initiates the compensation action in the Upd_Backend transaction.
The following happens after the orchestration receives the input message:
The UpdateCrm assembly updates a row in the Northwind database.
The UpdateMailingList assembly updates a matching record in the BTSCompensationSampleMailingList database.
In the event of a failure while updating the Northwind database, an exception is raised and the orchestration exits the process.
In the event of a failure while updating the BTSCompensationSampleMailingList database, an exception is raised and a ten-second delay takes place before rewriting the original customer data back to the Northwind database.
The following table shows the files in this sample and describes their purpose.
Batch file used to uninstall the sample.
Binding data for the orchestration (used during installation).
Contact information message schema.
Orchestration Designer layout file.
SQL script to create and populate the sample database.
CRM update message schema.
Mailing list message update schema.
Microsoft Visual Basic Scripting Edition (VBScript) script to remove Web service virtual and physical directories (used during uninstallation).
Message map to translate from request (contact info) to CRM update message.
Message map to translate from request (contact info) to mailing list message.
Batch file to install this sample.
Message map to translate from request (contact info) to a response message.
Response message schema.
InfoPath\Contact Info Update.xsn
Microsoft InfoPath file used to submit forms to the orchestration Web service.
Assembly information source file for the UpdateCrm assembly. (The UpdateCrm assembly updates the Northwind database.)
Main source code for the UpdateCrm assembly.
UpdateCrm project file.
Assembly information source file for the UpdateMailingList assembly. (The UpdateMailingList assembly updates the sample database.)
Main source code for the UpdateMailingList assembly.
UpdateMailingList project file.
To build and initialize the Compensation sample
In a Visual Studio command window, navigate to the following folder:
Run Setup.bat, which performs the following actions:
Build and deploy the sample assembly.
When the BizTalk Web Services Publishing Wizard launches, do the following manually:
On the Welcome to the BizTalk Web Services Publishing Wizard page, click Next.
On the Create Web Service page, select Publish BizTalk orchestration as web services, and then click Next.
On the BizTalk Assembly page, browse and select <Samples Path>\Orchestrations\Compensation\bin\Release\CompensationOrchestration.dll, and then click Next.
On the Orchestrations and Ports page, click Next.
On the Web Service Properties page, in Target namespace of web service, type http://Microsoft.BizTalk.Samples.Compensation/, and then click Next.
On the Web Service Project page, in Location, type http://localhost/CompensationOrchestrationWebServiceProxy.
Select the Allow anonymous access to web service check box.
Select the Create BizTalk receive location in the following application check box.
In the Create BizTalk receive location in the following application drop-down menu, select BizTalk Application 1 from the drop-down list, and then click Next.
On the Web Service Project Summary page, click Create.
On the Completing the BizTalk Web Services Publishing Wizard page, click Finish.
Setup creates and binds ports, creates the back-end database for the sample, and adds C# assemblies to the global assembly cache.
You should confirm that BizTalk Server did not report any errors during the build and initialization process before attempting to run this sample.
After you build and initialize this sample, consider the following before you run it:
If you are running this sample on Windows Server 2008 SP2 or Windows Server 2008 R2, you must create an IIS application pool and set its identity to an account that is a member of the BizTalk Application Users Windows group. You also need to update the orchestration Web service virtual directory to run within this application pool.
Add the ASPNET account to the BizTalk Isolated Host Users group.
Give the BizTalk Application Users group db_owner permission to the BTSCompensationSampleMailingList and Northwind databases.
If BizTalk Server is not installed in the default location (drive:\Program Files\Microsoft BizTalk Server <version>\), you must publish the Contact Info Update.xsn form before using it. To do so, do the following.
To publish the InfoPath form
In Internet Explorer, on the Tools menu, click Internet Options.
On the Security tab, click Internet, and then click Custom Level.
In the Miscellaneous section, ensure that the Access data sources across domains setting is enabled, and then click OK. This setting is required for the InfoPath user interface solution scripting code to run.
In Windows Explorer, navigate to <Samples Path>\Orchestrations\Compensation\InfoPath, right-click Contact Info Update.xsn and then click Design.
The InfoPath Contact Info Update solution opens in design mode.
In the InfoPath Contact Info Update application, on the File menu, click Publish.
The Publish Wizard appears.
Select To a shared folder on this computer or on a network and publish the solution to the path <Samples Path>\Orchestrations\Compensation\InfoPath\Contact Info Update.xsn.
Close the design mode InfoPath.
You are ready to run this sample.
To run the Compensation sample
Double-click Contact Info Update.xsn to open it in InfoPath.
Fill out the form for an account that exists in both databases. For example, use an existing account ID "ALFKI" from the Northwind Customers table.
On the File menu, select Submit, and click Submit.
The response document should appear in the <Samples Path>\Orchestrations\Compensation\Out folder, and both the Northwind and the BTSCompensationSampleMailingList databases should be updated with the new data from the InfoPath form.
You can detach the BTSCompensationSampleMailingList database or take it offline to test the compensation action that the orchestration performs. Observe that the record is updated in the Northwind database first. Then, when the orchestration tries to update the BTSCompensationSampleMailingList database, because that database is detached, the update fails. Therefore, an exception is raised and there is a ten-second delay before the compensation action takes place to write the original customer data back to the Northwind database.
You may get a "Login failed for user 'IIS APPPOOL\DefaultAppPool' error. It might be because of the token-based server access validation failure. To resolve this error, create a new application pool and use the administrator account in it.
To uninstall the Compensation sample
In a Visual Studio command window, navigate to the following folder: