Export (0) Print
Expand All

How to: Add Explicit Transactions to a Domain Service

WCF RIA Services

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

This topic explains how you can use explicit transactions in your WCF RIA Services domain service to guarantee that a set of changes are performed as a group. When you call SubmitChanges() from the client on the DomainContext, it calls Submit on the server. So transactions can be incorporated by overriding Submit(ChangeSet) method with the base implementation and wrapping it in a transaction scope. This, in turn, calls PersistChangeSet which finalizes all the CUD (Create, Update and Delete) operations that have been invoked.

This procedure is only required to integrate transactions into a domain service that derives directly from the DomainService class, as is the case, for example, when working with POCO (Plain Old CLR Objects) defined data models. When the domain service is derive from LinqToEntitiesDomainService<TContext> using Entity Framework, the transaction is handled automatically.

The procedure described in this topic assumes that you have a RIA Services application created. The procedures for doing this can be followed in the Walkthrough: Creating a RIA Services Solution topic.

To create an explicit transaction

  1. Right-click the server project and select the Add Reference option to open the Add Reference window. On the .NET tab, find and select the System.Transactions assembly and then click OK.

  2. In the domain service, override the Submit method.

  3. Create a transaction scope that wraps a call to the base implementation of the Submit method.

  4. Check the HasError property on the ChangeSet object before completing the transaction.

    The following example shows an explicit transaction for a domain service.

    [EnableClientAccess()]
    public class DomainService1 : DomainService
    {
        public override bool Submit(ChangeSet changeSet)
        {
            bool result;
    
            using (var tx = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })
                )
            {
                result = base.Submit(changeSet);
                if (!this.ChangeSet.HasError)
                {
                    tx.Complete();
                }
            }
    
            return result;
        }
    }
    
    
    
Show:
© 2015 Microsoft