While doing MSMQ there can be scenarios in the project, where we would like all the messages are uploaded to either MSMQ or all messages read from MSMQ. After any message is read from MSMQ, it is deleted from the queue. Therefore, some times this can be critical if there are exceptions in reading or uploading messages. As said before WCF transaction can be applied to database as well as other operations like MSMQ. So let us try to understand the same by doing a small sample.
Below is a numbered code snippet of MSMQ transactions.
1 - Send message is the exposed method in the service. We have marked this method as TransactionScopeRequired=true
2 - On the client side we have use the transaction scope to say that we are starting a transaction to MSMQ server. You can see we have used the send Message to send the messages to MSMQ.
3- We can then use Complete or Dispose method to complete or rollback the transactions.
The below MSMQ transaction code ensures that either all messages are send to the server or none are sent. Thus satisfying consistency property of transactions.
Figure 27:- Transaction snippet code
Asked In: Many Interviews |