Skip to main content

Transactions

1. Open the WCF service application that you created earlier or download start file here.

In Service Project

2. Update the OperationContract to include a TransactionFlow attribute. This ensure a transaction is required to invoke the SubmitReview method. 

        [TransactionFlow(TransactionFlowOption.Mandatory)]
        [OperationContract]

        void SubmitReview (ProductReview pr);

3. In the service implementation for SubmitReview, modify OperationBehavior attribute as shown below:

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]

void IProductService.SubmitReview(ProductReview pr)

4. Add reference to System.Messaging and add using System.Messaging;

5. In addition to writing to an XML file, lets also write to a transactional queue. First we need to create a queue.



6. Open Computer Management, expand Services and Applications, Expand Message Queuing > right click on Private Queues > select New > Private Queue > type productreview > check Transactional > click OK.

7. To write to the productreview message queue, update the submit review method as shown below:

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
void IProductService.SubmitReview(ProductReview pr)
{
    MessageQueue queue = new MessageQueue(@".\Private$\productreview");

    queue.Send(pr, MessageQueueTransactionType.Automatic);

...


In Service Host Project

8. Go to App.config of the Service Host and change the basicHttpBinding endpoint to use WsHttpBinidng and WsHttpBinding configuration should be updated as given below, to allows the transaction flow to the service.
        <bindings>
          <wsHttpBinding>
            <binding name ="WsHttpBindingConfig" transactionFlow="true">
              <reliableSession enabled="true" />
            </binding>
          </wsHttpBinding>

        </bindings>

9. Set the service Host as start up project and test the Service Host.

In Client Project

10. Update Service Reference of the client

11. In the client, update main() to uses the WSHttpBinding instead of BasicHttpBinding

ProductServiceClient client = new ProductServiceClient("WSHttpBinding_IProductService");

12. Add reference to System.Transactions and add using System.Transactions;

13. In the main method of client wrap the method invocation using TransactionScope as shown below this makes the transaction to be flowed to service.

using (TransactionScope trans = new TransactionScope())
{
   client.SubmitReview(pr);
   trans.Complete();

}

Test

14. Run both Host and Client

15.  Check the queue to see if the message is queued. Open the queue and examine the body of the queue.

16. Now comment the trans.Complete(); and then run host and client. Add a product review and close both client and host. Now check the message queue. The new product review should not be in the queue since the transaction was not completed and this means transaction has failed and all that is executed within the transaction is rolled back.

Links and References

Transactions in WCF Services https://msdn.microsoft.com/en-us/library/ff384250.aspx

Comments

Popular posts from this blog

CUMIPMT and CUMPRINC function

CUMIPMT Cumulative interest payment function allows you to calculate the interest paid for a loan or from an investment from period A to period B. When getting a loan, CUMIPMT function can be used to calculate the total amount of interest paid in the first five months or from period 12 to period 20. A period can be a month, a week or two week. Loan Amount : 350,000.00 APR: 4.5% Down payment: 0.00 Years: 25 Payment per year: 12 From the above data, we can calculate the following: No of Period: 25 × 12 = 300 Periodic Rate: 4.5/12 = 0.375% Here is how you will substitute these values into the function. = CUMIPMT (periodic rate, No of period, vehicle price, start period, end period,  ) = CUMIPMT (0.375, 300, 350000, 1, 5, 0) In an excel worksheet, we use cell address instead of actual values as shown below: Here is the formula view of the worksheet: CUMPRINC Another related function is CUMPRINC. CUMPRINC function is used to calculate cumul

Excel PMT Function

PMT function is very useful for calculating monthly payment required to payback a loan or mortgage at a fixed rate. This function require a minimum of three inputs, periodic rate, number of periods, present value or the loan amount. Here is a simple example. Home Loan: 350,000.00 Interest rate: 4.5% Number of years to repay the loan: 25 Note: To calculate monthly payment, we need to find the monthly rate and number of months as shown above. Then it is simply a matter of substituting the values into the payment function, as shown in the formula view below.

BCG's Brand Advocacy Index

The Boston Consulting Group's (BCG) Brand Advocacy Index (BAI) is a metric developed to help companies measure the degree of customer advocacy for their brands. BAI focuses on the likelihood of customers to recommend a brand to others, which is a powerful indicator of brand strength and customer loyalty. Unlike other customer satisfaction or loyalty metrics, BAI emphasizes the importance of customer referrals and word-of-mouth marketing. BAI is calculated based on a survey where customers are asked about their willingness to recommend a brand to their friends, family, or colleagues. The responses are then used to compute a score, which ranges from -100 to 100. A higher BAI score indicates that a brand has more advocates who are likely to recommend the brand to others, while a lower score suggests that the brand has fewer advocates or even a higher number of detractors. BCG's research has shown that companies with higher BAI scores tend to experience higher growth rates and bett