In windows azure, we hosted two asp.net webapi projects as an application. Here you need to enable a distributed transaction. We initiate a transaction within the same api. Then, inside this transaction area, we get the distribution token of this transaction and send it as a header during another api call. The code is something like below.
[HttpGet] [Route("api/Test/Transaction/Commit")] public async Task<string> Commit() { using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }, TransactionScopeAsyncFlowOption.Enabled)) {
Inside the api (... api / Test / Transaction / NoCommit), to which we make a call inside the transaction area, we select the marxed transaction distribution token from the header and use this to create an instance of this transaction and create an instance of TransactionScope using this transaction. Later we use this transaction area to complete this transaction. We applied an action filter to apply this and added this filter to the action that is responsible for this api call. The code for this api and action filter is something like below.
[HttpGet] [EnlistToDistributedTransactionActionFilter] [Route("api/Test/Transaction/NoCommit")] public async Task<string> NoCommit() { this.Repository.Insert(new Client { Name = "Test", AllowedOrigin = "*", Active = true, ClientGuid = Guid.NewGuid(), RefreshTokenLifeTime = 0, ApplicationType = ApplicationTypes.JavaScript, Secret = "ffff", Id = "Test" } ); await this.Repository.SaveChangesAsync(); return "value"; } public class EnlistToDistributedTransactionActionFilter : ActionFilterAttribute { private const string TransactionId = "TransactionToken";
So, if during this call any exception (api / Test / Transaction / Commit) occurs inside this transaction area (either in the firt api or in the second api), all database changes made by both api will be thrown back. This works fine on the spot. Since locally we get MSDTC support. But there is no MSDTC support in Azure. In azure, we get support from Elastic transaction. Because of this, when we try to get the transaction propagation token from the first server, we get an exception. Therefore, when we try to execute the following code var transaction = TransactionInterop.GetTransactionFromTransmitterPropagationToken (transactionToken); We get an exception with the message "The value does not fall into the expected range . " This is a post saying that this method will require promotion in the MSDTC System.Transactions, but for an elastic transaction, how do we do it? For an elastic transaction, we need to march the transaction into a distribution token. How to do it? Search for a solution.
azure transactions transactionscope msdtc distributed-transactions
Anup
source share