Even that Transactions are not recommended on MicroServices(that usually is a sign that your services were mistakenly splitted), there are some situations(usually legacy calls) that demans some transaction control...
REMARK: The .Net Code/.Net 5 service is not working, still... :-| More Infos: //dotnet/runtime#715
Sample using Transactions(via MSDTC) on Rest Calls to .Net Framework Service(Via System.Transactions)
Created based on this article: https://www.c-sharpcorner.com/article/distributed-transactions-with-webapi-across-application-domains/
- Create a Database with a table named LogTable with this structure:
CREATE TABLE [dbo].[LogTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
CONSTRAINT [PK_LogTable] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-
If needed, just update the DB connectionstring on the NetFrwService\Web.Config
-
Start the solution with both Projects(Multiple) set as Startup Project. Press enter on the console application. It will run a loop of 2 calls to the service withina transactionscope. Between the calls it is waiting 10 seconds... Between calls you can see the table is locked and that only everythign is fine, the Transaction will be commited.
#The important Bits What really matters is:
-
When the HTTP Client makes the request, we inject a header named "TransactionToken" with Transaction Propagation Token Serialized as Base64:
MicroService.2PC.Transaction/DotNetCoordinator/Extensions.cs
Lines 13 to 14 in c04e9dc
-
When the Rest Service receives the request, it checks for the "TransactionToken" header and creates a transaction Scope connected with the transaction started on the Caller. We do this over a Filter:
Note that the Controller Action that needs to be "transactionable" have an attribute(EnlistToDistributedTransactionActionFilter) to use the filter:
Obviously, both machines needs to have access to the MSDTC which is coordinating the transaction. Otherwise it will not work.
ENJOY