{"id":32330,"date":"2016-02-08T11:47:03","date_gmt":"2016-02-08T06:17:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32330"},"modified":"2016-02-09T11:10:12","modified_gmt":"2016-02-09T05:40:12","slug":"enable-transaction-in-wcf","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/enable-transaction-in-wcf\/","title":{"rendered":"Enable Transaction in WCF"},"content":{"rendered":"<p>Transaction in WCF is a set of operations or logical unit of work comprising of activities. Here, if a single operation fails, then all operation fails automatically.<\/p>\n<p style=\"background: white\"><strong><span style=\"font-size: 9.0pt;font-family: 'Verdana','sans-serif';color: #444444;background: white\">We will follow these steps for enable transaction in WCF :<\/span><\/strong><\/p>\n<p><strong>Step 1:-<\/strong> Add\u00a0 Transactions\u00a0namespace in \u00a0WCF Service project.<\/p>\n<p>Using System.Transaction;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:-<\/strong> Set\u00a0TransactionFlow\u00a0property of the OperationContract attribute\u00a0Mandatory.<\/p>\n<p>options for TransactionFlow are as follows:<br \/>\na. Mandatory \u2013 transaction must be flowed<br \/>\nb. Allowed \u2013 transaction may be flowed<br \/>\nc. Not Allowed \u2013 transaction is not flowed<\/p>\n<p>Our WCF service contract:<\/p>\n<p>[TransactionFlow(TransactionFlowOptions.Mandatory]<br \/>\nvoid Showinfo();<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3:-<\/strong> Now, set the\u00a0OperationBehavior\u00a0attribute for the method.<\/p>\n<p>[OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=true)]<br \/>\nvoid showinfo()<br \/>\n{<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>TransactionScopeRequired\u00a0= true (it can only be called in a transaction)<br \/>\nTransactionAutoComplete\u00a0= true (if the operation completes successfully, transaction will be committed.)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:-<\/strong> Enable Transactions for WCF\u00a0Binding being used.<br \/>\nFor Example, In our configuration file (Web.config) bindings will be as follows:<\/p>\n<p>&lt;bindings&gt;<\/p>\n<p>&lt;wsHttpBinding&gt;<br \/>\n&lt;binding name=\u201dhttpBinding\u201d\u00a0\u00a0transactionFlow=\u201dtrue\u201d\u00a0\/&gt;<br \/>\nwsHttpBinding &gt;<br \/>\nNote:- That we must choose a binding that supports transactions As Follow<\/p>\n<ul>\n<li>netTcpBinding<\/li>\n<li>netNamedPipeBinding<\/li>\n<li>wsHttpBinding<\/li>\n<li>wsDualHttpBinding<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Step 5:-<\/strong> Need to start the transaction from client side<\/p>\n<p>Import namspance transaction in WCF<\/p>\n<p>using System.Transaction;<br \/>\nUsing(TransactionScope \u00a0transScope = new TransactionScope())<br \/>\n{<br \/>\nIMyServiceClient client = new IMyServiceClient();<br \/>\nclient.showinfo();<\/p>\n<p>transScope.complete();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transaction in WCF is a set of operations or logical unit of work comprising of activities. Here, if a single operation fails, then all operation fails automatically. We will follow these steps for enable transaction in WCF : Step 1:- Add\u00a0 Transactions\u00a0namespace in \u00a0WCF Service project. Using System.Transaction; &nbsp; Step 2:- Set\u00a0TransactionFlow\u00a0property of the OperationContract [&hellip;]<\/p>\n","protected":false},"author":855,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[1440,3064],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32330"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/855"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32330"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32330\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}