Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transactions with ASP.net WEB API

I understand that ASP.NET WEB API has been built to help implement lightweight REST based applications. However, i need my REST services to be transactional/be part of a transaction. I tried looking around, but it looks like there is no way to enlist WEB APIs as part of a client initiated transaction. Is there a way to do this?

regards Jagadish

like image 303
user1496864 Avatar asked Jul 02 '12 18:07

user1496864


2 Answers

If you control both ends of the wire, it's possible to achieve what you want.

The TransactionInterop class exists to provide support for working with transactions between process boundaries, leveraging MS DTC.

It contains two methods which are interesting to your scenario:

  • GetTransmitterPropagationToken - This gets a token which can be used to propagate the transaction outside of the current process.

  • GetTransactionFromTransmitterPropagationToken - This takes a propagation token and uses it to construct the transaction in the local service.

You can use the first method in your client to generate a transaction. You can set it as a custom header value or a cookie to pass it to your service. Once on the service, you can use the second method to create the transaction locally.

The major caveat to this is that MS DTC will need to be enabled and configured at client and server. This is only really achievable if your services are being invoked within a Windows Active Directory Domain.

like image 102
Paul Turner Avatar answered Oct 20 '22 00:10

Paul Turner


I believe you are referring to distributed transactions (via MSDTC) which can propagate over service boundaries.

However, distributed transactions over WCF RESTful services are not possible because there is simply no way to propagate and manage the transaction state over plain HTTP requests.

You may want to look into plain WCF services, over HTTP (wsHttpBinding) or TCP/IP (net.tcp), or even give a look on WCF Data Services.

like image 25
Marcel N. Avatar answered Oct 19 '22 22:10

Marcel N.