├── .gitignore
├── LICENSE
├── NTccTransactionCore.sln
├── README.md
├── TCC Architecture.png
├── samples
├── NTccTransaction.Http.Capital.WebApi
│ ├── Controllers
│ │ └── CapitalController.cs
│ ├── NTccTransaction.Http.Capital.WebApi.csproj
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── Services
│ │ ├── CapitalService.cs
│ │ └── ICapitalService.cs
│ ├── Startup.cs
│ ├── appsettings.Development.json
│ ├── appsettings.json
│ └── log4net.config
└── NTccTransaction.Http.Order.WebApi
│ ├── Controllers
│ └── OrderController.cs
│ ├── ICapitalApi.cs
│ ├── NTccTransaction.Http.Order.WebApi.csproj
│ ├── Program.cs
│ ├── Properties
│ └── launchSettings.json
│ ├── Proxy
│ └── CapitalProxy.cs
│ ├── Services
│ ├── IOrderService.cs
│ ├── IOrderService1.cs
│ ├── OrderService.cs
│ └── OrderService1.cs
│ ├── Startup.cs
│ ├── appsettings.Development.json
│ ├── appsettings.json
│ └── log4net.config
├── src
├── NTccTransaction.Abstractions
│ ├── Check.cs
│ ├── Exception
│ │ ├── CancellingException.cs
│ │ ├── ConcurrencyTransactionException.cs
│ │ ├── ConfirmingException.cs
│ │ ├── NoExistedTransactionException.cs
│ │ ├── TransactionException.cs
│ │ └── TryingException.cs
│ ├── ICompensable.cs
│ ├── INTccInterceptor.cs
│ ├── INTccTransactionOptionsExtension.cs
│ ├── IUniqueIdentity.cs
│ ├── Internal
│ │ ├── CacheExecutorDescriptor.cs
│ │ ├── IBootstrapper.cs
│ │ ├── IMethodInvocation.cs
│ │ ├── INTccTransactionService.cs
│ │ ├── IProcessingServer.cs
│ │ ├── ITransactionContextEditor.cs
│ │ ├── ITransactionMethodInvoker.cs
│ │ ├── ITransactionRecoveryProcessingServer.cs
│ │ ├── ITransactionServiceSelector.cs
│ │ └── InvocationContext.cs
│ ├── MethodRole.cs
│ ├── NTccTransaction.Abstractions.csproj
│ ├── Persistence
│ │ ├── IAmbientTransaction.cs
│ │ ├── IParticipant.cs
│ │ ├── ITransaction.cs
│ │ ├── ITransactionAccessor.cs
│ │ ├── ITransactionManager.cs
│ │ ├── ITransactionRecovery.cs
│ │ └── ITransactionRepository.cs
│ ├── Propagation.cs
│ ├── Serialization
│ │ └── ISerializer.cs
│ ├── System
│ │ ├── Collections
│ │ │ └── Generic
│ │ │ │ ├── HashSetExtensions.cs
│ │ │ │ └── StackExtension.cs
│ │ └── Reflection
│ │ │ └── MethodBaseExtensions.cs
│ ├── TransactionContext.cs
│ ├── TransactionStatus.cs
│ ├── TransactionType.cs
│ └── TransactionXId.cs
├── NTccTransaction.Aop
│ ├── AutofacRegistration.cs
│ ├── CastleAsyncInterceptorAdapter.cs
│ ├── CastleMethodInvocationAdapter.cs
│ ├── CastleMethodInvocationAdapterBase.cs
│ ├── CastleMethodInvocationAdapterWithReturnValue.cs
│ ├── CompensableTransactionInterceptor.cs
│ ├── FodyWeavers.xml
│ ├── FodyWeavers.xsd
│ ├── NTccTransaction.Aop.csproj
│ ├── NTccTransaction.AopOptionsExtension.cs
│ ├── NTccTransaction.Options.Extensions.cs
│ ├── ResourceCoordinatorInterceptor.cs
│ └── TccAsyncDeterminationInterceptor.cs
├── NTccTransaction.Oracle
│ ├── DBScript
│ │ └── NTccTransaction.sql
│ ├── NTccTransaction.Oracle.csproj
│ ├── NTccTransaction.cs
│ ├── Options
│ │ ├── DbContextOptionsFactory.cs
│ │ ├── NTccTransaction.EFOptions.cs
│ │ ├── NTccTransaction.Options.Extensions.cs
│ │ ├── NTccTransaction.OracleOptions.cs
│ │ └── NTccTransaction.OracleOptionsExtension.cs
│ ├── TransactionDbContext.cs
│ └── TransactionRepository.cs
├── NTccTransaction.SqlServer
│ ├── DBScript
│ │ └── NTccTransaction.sql
│ ├── NTccTransaction.SqlServer.csproj
│ ├── NTccTransaction.cs
│ ├── Options
│ │ ├── DbContextOptionsFactory.cs
│ │ ├── NTccTransaction.EFOptions.cs
│ │ ├── NTccTransaction.Options.Extensions.cs
│ │ ├── NTccTransaction.SqlServerOptions.cs
│ │ └── NTccTransaction.SqlServerOptionsExtension.cs
│ ├── TransactionDbContext.cs
│ └── TransactionRepository.cs
└── NTccTransaction
│ ├── CompensableAttribute.cs
│ ├── CompensableMethodContext.cs
│ ├── Internal
│ ├── DefaultTransactionContextEditor.cs
│ ├── IBootstrapper.Default.cs
│ ├── ITransactionMethodInvoker.Default.cs
│ ├── ITransactionRecoveryProcessingServer.Default.cs
│ ├── ITransactionServiceSelector.Default.cs
│ ├── MethodMatcherCache.cs
│ └── ObjectMethodExecutor
│ │ ├── AwaitableInfo.cs
│ │ ├── CoercedAwaitableInfo.cs
│ │ ├── ObjectMethodExecutor.cs
│ │ ├── ObjectMethodExecutorAwaitable.cs
│ │ └── ObjectMethodExecutorFSharpSupport.cs
│ ├── NTccTransaction.Builder.cs
│ ├── NTccTransaction.ServiceCollectionExtensions.cs
│ ├── NTccTransaction.csproj
│ ├── NTccTransactionOptions.cs
│ ├── Persistence
│ ├── AmbientTransaction.cs
│ ├── CachableTransactionRepository.cs
│ ├── Participant.cs
│ ├── Transaction.cs
│ ├── TransactionManager.cs
│ └── TransactionRecovery.cs
│ ├── Serialization
│ └── StringSerializer.cs
│ └── Utils
│ ├── CompensableHelper.cs
│ ├── NTccContractResolver.cs
│ └── TransactionUtils.cs
└── test
└── NTccTransaction.Test
├── LocalService
└── LocalServiceUnitTest.cs
└── NTccTransaction.Test.csproj
/.gitignore:
--------------------------------------------------------------------------------
1 | /.vs
2 | bin/
3 | obj/
4 | /packages
5 | *.user
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 wzl-bxg
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/NTccTransactionCore.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30717.126
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{816CAAED-2B80-47B7-85F4-A1222F9E1888}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{A1AD4F79-2D56-40B2-AF73-7C1AFDFD6D9E}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{F77EE4FB-4501-4CE4-A92B-72259171F206}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction", "src\NTccTransaction\NTccTransaction.csproj", "{8A46B1E4-1A6C-4F9D-BF2B-75BA574757E1}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction.Oracle", "src\NTccTransaction.Oracle\NTccTransaction.Oracle.csproj", "{2B83E6DA-EBF2-4067-BBB2-F5126BE6C1B7}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction.Aop", "src\NTccTransaction.Aop\NTccTransaction.Aop.csproj", "{61A42BA6-8DDB-4AD8-B053-5268BD9066FC}"
17 | EndProject
18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction.Abstractions", "src\NTccTransaction.Abstractions\NTccTransaction.Abstractions.csproj", "{6827958D-2CC2-4F96-AB18-7579ED94D0D4}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction.Http.Order.WebApi", "samples\NTccTransaction.Http.Order.WebApi\NTccTransaction.Http.Order.WebApi.csproj", "{B64BFC0D-C524-4A44-B92C-119EF4A474BD}"
21 | EndProject
22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction.Test", "test\NTccTransaction.Test\NTccTransaction.Test.csproj", "{79A7395F-2665-4821-A587-07FD2A381DCB}"
23 | EndProject
24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTccTransaction.Http.Capital.WebApi", "samples\NTccTransaction.Http.Capital.WebApi\NTccTransaction.Http.Capital.WebApi.csproj", "{6241B896-5234-4684-9B42-0CAC8E8D5D00}"
25 | EndProject
26 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NTccTransaction.SqlServer", "src\NTccTransaction.SqlServer\NTccTransaction.SqlServer.csproj", "{8A4EEB06-08AB-41BF-904F-04135DC6F42A}"
27 | EndProject
28 | Global
29 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
30 | Debug|Any CPU = Debug|Any CPU
31 | Release|Any CPU = Release|Any CPU
32 | EndGlobalSection
33 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
34 | {8A46B1E4-1A6C-4F9D-BF2B-75BA574757E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35 | {8A46B1E4-1A6C-4F9D-BF2B-75BA574757E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
36 | {8A46B1E4-1A6C-4F9D-BF2B-75BA574757E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 | {8A46B1E4-1A6C-4F9D-BF2B-75BA574757E1}.Release|Any CPU.Build.0 = Release|Any CPU
38 | {2B83E6DA-EBF2-4067-BBB2-F5126BE6C1B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39 | {2B83E6DA-EBF2-4067-BBB2-F5126BE6C1B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
40 | {2B83E6DA-EBF2-4067-BBB2-F5126BE6C1B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
41 | {2B83E6DA-EBF2-4067-BBB2-F5126BE6C1B7}.Release|Any CPU.Build.0 = Release|Any CPU
42 | {61A42BA6-8DDB-4AD8-B053-5268BD9066FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43 | {61A42BA6-8DDB-4AD8-B053-5268BD9066FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
44 | {61A42BA6-8DDB-4AD8-B053-5268BD9066FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
45 | {61A42BA6-8DDB-4AD8-B053-5268BD9066FC}.Release|Any CPU.Build.0 = Release|Any CPU
46 | {6827958D-2CC2-4F96-AB18-7579ED94D0D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47 | {6827958D-2CC2-4F96-AB18-7579ED94D0D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
48 | {6827958D-2CC2-4F96-AB18-7579ED94D0D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
49 | {6827958D-2CC2-4F96-AB18-7579ED94D0D4}.Release|Any CPU.Build.0 = Release|Any CPU
50 | {B64BFC0D-C524-4A44-B92C-119EF4A474BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51 | {B64BFC0D-C524-4A44-B92C-119EF4A474BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
52 | {B64BFC0D-C524-4A44-B92C-119EF4A474BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
53 | {B64BFC0D-C524-4A44-B92C-119EF4A474BD}.Release|Any CPU.Build.0 = Release|Any CPU
54 | {79A7395F-2665-4821-A587-07FD2A381DCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55 | {79A7395F-2665-4821-A587-07FD2A381DCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
56 | {79A7395F-2665-4821-A587-07FD2A381DCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
57 | {79A7395F-2665-4821-A587-07FD2A381DCB}.Release|Any CPU.Build.0 = Release|Any CPU
58 | {6241B896-5234-4684-9B42-0CAC8E8D5D00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59 | {6241B896-5234-4684-9B42-0CAC8E8D5D00}.Debug|Any CPU.Build.0 = Debug|Any CPU
60 | {6241B896-5234-4684-9B42-0CAC8E8D5D00}.Release|Any CPU.ActiveCfg = Release|Any CPU
61 | {6241B896-5234-4684-9B42-0CAC8E8D5D00}.Release|Any CPU.Build.0 = Release|Any CPU
62 | {8A4EEB06-08AB-41BF-904F-04135DC6F42A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63 | {8A4EEB06-08AB-41BF-904F-04135DC6F42A}.Debug|Any CPU.Build.0 = Debug|Any CPU
64 | {8A4EEB06-08AB-41BF-904F-04135DC6F42A}.Release|Any CPU.ActiveCfg = Release|Any CPU
65 | {8A4EEB06-08AB-41BF-904F-04135DC6F42A}.Release|Any CPU.Build.0 = Release|Any CPU
66 | EndGlobalSection
67 | GlobalSection(SolutionProperties) = preSolution
68 | HideSolutionNode = FALSE
69 | EndGlobalSection
70 | GlobalSection(NestedProjects) = preSolution
71 | {8A46B1E4-1A6C-4F9D-BF2B-75BA574757E1} = {816CAAED-2B80-47B7-85F4-A1222F9E1888}
72 | {2B83E6DA-EBF2-4067-BBB2-F5126BE6C1B7} = {816CAAED-2B80-47B7-85F4-A1222F9E1888}
73 | {61A42BA6-8DDB-4AD8-B053-5268BD9066FC} = {816CAAED-2B80-47B7-85F4-A1222F9E1888}
74 | {6827958D-2CC2-4F96-AB18-7579ED94D0D4} = {816CAAED-2B80-47B7-85F4-A1222F9E1888}
75 | {B64BFC0D-C524-4A44-B92C-119EF4A474BD} = {F77EE4FB-4501-4CE4-A92B-72259171F206}
76 | {79A7395F-2665-4821-A587-07FD2A381DCB} = {A1AD4F79-2D56-40B2-AF73-7C1AFDFD6D9E}
77 | {6241B896-5234-4684-9B42-0CAC8E8D5D00} = {F77EE4FB-4501-4CE4-A92B-72259171F206}
78 | {8A4EEB06-08AB-41BF-904F-04135DC6F42A} = {816CAAED-2B80-47B7-85F4-A1222F9E1888}
79 | EndGlobalSection
80 | GlobalSection(ExtensibilityGlobals) = postSolution
81 | SolutionGuid = {59092443-14B5-4593-B1D3-6E102F01E4F4}
82 | EndGlobalSection
83 | EndGlobal
84 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # NTcc-TransactionCore - The .NET Core port based on tcc-transaction
2 |
3 |
4 | ## Introduction
5 |
6 | This is the README file for NTcc-TransactionCore, .NET Core port of Java tcc-transaction. It supports .NET Core/netstandard 2.0 and later, not supported for .NET Framework.
7 |
8 | NTcc-TransactionCore is an opensource project aimed at creating a free-for-commercial use TCC transaction, with enterprise features.
9 |
10 | ## Architecture overview
11 | 
12 |
13 | ## Getting Started
14 |
15 | ### NuGet
16 |
17 | NTcc-TransactionCore can be installed in your project with the following command.
18 |
19 | ~~~shell
20 | PM> Install-Package NTccTransactionCore
21 | PM> Install-Package NTccTransactionCore.Aop
22 | ~~~
23 |
24 | NTcc-TransactionCore Currently supports Oracle, SqlServer as transaction log storage, following packages are available to install:
25 |
26 | ~~~shell
27 | PM> Install-Package NTccTransactionCore.Oracle
28 | PM> Install-Package NTccTransactionCore.SqlServer
29 | ~~~
30 |
31 | ### Configuration
32 |
33 | First, you need to configure NTcc-TransactionCore in your `Startup.cs`:
34 |
35 | ~~~c#
36 | public ILifetimeScope AutofacContainer { get; private set; }
37 |
38 | public IServiceCollection Services { get; private set; }
39 |
40 | public void ConfigureServices(IServiceCollection services)
41 | {
42 | //......
43 |
44 | services.AddNTccTransaction((option) =>
45 | {
46 | option.UseOracle((oracleOption) =>
47 | {
48 | oracleOption.ConnectionString = Configuration.GetConnectionString("Your ConnectionStrings");// configure db connectiong
49 | });
50 |
51 | option.UseCastleInterceptor(); // use Castle Interceptor
52 | });
53 |
54 | Services = services;
55 | }
56 |
57 | public void ConfigureContainer(ContainerBuilder containerBuilder)
58 | {
59 | containerBuilder.Register(Services); // register Castle Interceptor
60 | }
61 |
62 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
63 | {
64 | this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
65 | }
66 | ~~~
67 |
68 | ### DB Script
69 |
70 | Currently supports Oracle, SqlServer as transaction log storage, execute the following database script to create transaction table:
71 |
72 | #### Oracle
73 |
74 | ~~~sql
75 | CREATE TABLE NTCC_TRANSACTION
76 | (
77 | TRANSACTION_ID VARCHAR2(128) NOT NULL
78 | , GLOBAL_TRANSACTION_ID VARCHAR2(128)
79 | , BRANCH_QUALIFIER VARCHAR2(128)
80 | , STATUS NUMBER(9, 0) NOT NULL
81 | , TRANSACTION_TYPE NUMBER(9, 0) NOT NULL
82 | , RETRIED_COUNT NUMBER(9, 0) NOT NULL
83 | , CREATE_UTC_TIME DATE NOT NULL
84 | , LAST_UPDATE_UTC_TIME DATE NOT NULL
85 | , VERSION NUMBER(9, 0) NOT NULL
86 | , CONTENT CLOB
87 | , CONSTRAINT PK_NTCC_TRANSACTION PRIMARY KEY
88 | (
89 | TRANSACTION_ID
90 | )
91 | );
92 | ~~~
93 |
94 | #### Sql Server
95 |
96 | ~~~sql
97 | CREATE TABLE [dbo].[NTCC_TRANSACTION]
98 | (
99 | [TRANSACTION_ID] varchar(128) NOT NULL
100 | ,[GLOBAL_TRANSACTION_ID] varchar(128) NULL
101 | ,[BRANCH_QUALIFIER] varchar(128) NULL
102 | ,[STATUS] int NOT NULL
103 | ,[TRANSACTION_TYPE] int NOT NULL
104 | ,[RETRIED_COUNT] int NOT NULL
105 | ,[CREATE_UTC_TIME] datetime NOT NULL
106 | ,[LAST_UPDATE_UTC_TIME] datetime NOT NULL
107 | ,[VERSION] int NOT NULL
108 | ,[CONTENT] nvarchar(MAX) NULL
109 | ,PRIMARY KEY
110 | (
111 | [TRANSACTION_ID]
112 | )
113 | )
114 | ~~~
115 |
116 |
117 |
118 | ### In Business Logic Service
119 |
120 | In your business service, you need implement `INTccTransactionService`:
121 |
122 | ~~~c#
123 | public class OrderService : IOrderService, INTccTransactionService
124 | {
125 | private readonly ILogger _logger;
126 | private readonly ICapitalProxy _capitalProxy;
127 |
128 | public OrderService(ILogger logger, ICapitalProxy capitalProxy)
129 | {
130 | _logger = logger;
131 | _capitalProxy = capitalProxy;
132 | }
133 |
134 | [Compensable(CancelMethod = "CancelOrder", ConfirmMethod = "ConfirmOrder")]
135 | public async Task TryPostOrder(string input, TransactionContext transactionContext = null)
136 | {
137 | return await Task.FromResult("");
138 | }
139 |
140 | public async Task ConfirmOrder(string input, TransactionContext transactionContext = null)
141 | {
142 | await Task.CompletedTask;
143 | }
144 |
145 | public async Task CancelOrder(string input, TransactionContext transactionContext = null)
146 | {
147 | await Task.CompletedTask;
148 | }
149 | }
150 | ~~~
151 |
152 | And add the attribute `[Compensable(CancelMethod = "xxx", ConfirmMethod = "xxx")]` on the `Try` method:
153 |
154 | ~~~c#
155 | [Compensable(CancelMethod = "CancelOrder", ConfirmMethod = "ConfirmOrder")]
156 | public async Task TryPostOrder(string input, TransactionContext transactionContext = null)
157 | {
158 | return await Task.FromResult("");
159 | }
160 | ~~~
161 |
162 | The type of the last parameter of the `Try` method must be `TransactionContext`, it's used to propagate transactions, and you need add `Confirm` method and `Cancel` method in the business logic service,the parameters of the two methods must be same as `Try ` method.
163 |
164 | ~~~C#
165 | public async Task ConfirmOrder(string input, TransactionContext transactionContext = null)
166 | {
167 | await Task.CompletedTask;
168 | }
169 |
170 | public async Task CancelOrder(string input, TransactionContext transactionContext = null)
171 | {
172 | await Task.CompletedTask;
173 | }
174 | ~~~
175 |
176 | Then register your class that implement `INTccTransactionService` in `Startup.cs` :
177 |
178 | ```c#
179 | public void ConfigureServices(IServiceCollection services)
180 | {
181 | services.AddTransient();
182 | services.AddTransient();
183 | }
184 | ```
185 |
186 | ### Contribute
187 |
188 | One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.
--------------------------------------------------------------------------------
/TCC Architecture.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzl-bxg/NTcc-TransactionCore/6c58058a86d1fa28ac7d09d579c6215507b146d8/TCC Architecture.png
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/Controllers/CapitalController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.Extensions.Logging;
3 | using NTccTransaction.Abstractions;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Threading.Tasks;
8 |
9 | namespace NTccTransaction.Http.Capital.WebApi.Controllers
10 | {
11 | [ApiController]
12 | [Route("[controller]")]
13 | public class CapitalController : ControllerBase
14 | {
15 |
16 | private readonly ILogger _logger;
17 | private readonly ICapitalService _capitalService;
18 |
19 | public CapitalController(ILogger logger, ICapitalService capitalService)
20 | {
21 | _logger = logger;
22 | _capitalService = capitalService;
23 | }
24 |
25 | [HttpPost("[action]")]
26 | public async Task RecordAsync(TransactionContext transactionContext)
27 | {
28 | return await _capitalService.Record(transactionContext);
29 | }
30 |
31 | [HttpGet]
32 | public async Task get()
33 | {
34 | return await Task.FromResult("test");
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/NTccTransaction.Http.Capital.WebApi.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 | true
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/Program.cs:
--------------------------------------------------------------------------------
1 | using Autofac.Extensions.DependencyInjection;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.Configuration;
4 | using Microsoft.Extensions.Hosting;
5 | using Microsoft.Extensions.Logging;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Threading.Tasks;
10 |
11 | namespace NTccTransaction.Http.Capital.WebApi
12 | {
13 | public class Program
14 | {
15 | public static void Main(string[] args)
16 | {
17 | CreateHostBuilder(args).Build().Run();
18 | }
19 |
20 | public static IHostBuilder CreateHostBuilder(string[] args) =>
21 | Host.CreateDefaultBuilder(args)
22 | .UseServiceProviderFactory(new AutofacServiceProviderFactory())
23 | .ConfigureWebHostDefaults(webBuilder =>
24 | {
25 | webBuilder.UseStartup();
26 | });
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:62251",
8 | "sslPort": 0
9 | }
10 | },
11 | "profiles": {
12 | "IIS Express": {
13 | "commandName": "IISExpress",
14 | "launchBrowser": true,
15 | "launchUrl": "swagger",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "NTccTransaction.Http.Capital.WebApi": {
21 | "commandName": "Project",
22 | "dotnetRunMessages": "true",
23 | "launchBrowser": true,
24 | "launchUrl": "swagger",
25 | "applicationUrl": "http://localhost:5001",
26 | "environmentVariables": {
27 | "ASPNETCORE_ENVIRONMENT": "Development"
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/Services/CapitalService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Logging;
2 | using NTccTransaction.Abstractions;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 |
8 | namespace NTccTransaction.Http.Capital.WebApi
9 | {
10 | public class CapitalService : ICapitalService, INTccTransactionService
11 | {
12 | private readonly ILogger _logger;
13 |
14 | public CapitalService(ILogger logger)
15 | {
16 | _logger = logger;
17 | }
18 |
19 | [Compensable(CancelMethod = "CancelRecord", ConfirmMethod = "ConfirmRecord")]
20 | public async Task Record(TransactionContext transactionContext)
21 | {
22 | _logger.LogInformation("CapitalService.Record");
23 |
24 | return await Task.FromResult("Capital Return");
25 | }
26 |
27 | public void ConfirmRecord(TransactionContext transactionContext = null)
28 | {
29 | _logger.LogInformation("CapitalService.ConfirmRecord");
30 | }
31 |
32 | public void CancelRecord(TransactionContext transactionContext = null)
33 | {
34 | _logger.LogInformation("CapitalService.CancelRecord");
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/Services/ICapitalService.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 |
7 | namespace NTccTransaction.Http.Capital.WebApi
8 | {
9 | public interface ICapitalService
10 | {
11 | Task Record(TransactionContext transactionContext);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/Startup.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wzl-bxg/NTcc-TransactionCore/6c58058a86d1fa28ac7d09d579c6215507b146d8/samples/NTccTransaction.Http.Capital.WebApi/Startup.cs
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "ConnectionStrings": {
3 | "capitalDb": "Server=127.0.0.1;Database=capitalDb;User Id=sa;Password=12345678;"
4 | },
5 | "Logging": {
6 | "LogLevel": {
7 | "Default": "Information",
8 | "Microsoft": "Warning",
9 | "Microsoft.Hosting.Lifetime": "Information"
10 | }
11 | },
12 | "AllowedHosts": "*"
13 | }
14 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Capital.WebApi/log4net.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Controllers/OrderController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.Extensions.Logging;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 |
8 | namespace NTccTransaction.Http.Order.WebApi.Controllers
9 | {
10 | [ApiController]
11 | [Route("[controller]")]
12 | public class OrderController : ControllerBase
13 | {
14 |
15 |
16 | private readonly ILogger _logger;
17 |
18 | private readonly IOrderService _orderService;
19 |
20 | public OrderController(ILogger logger, IOrderService orderService)
21 | {
22 | _logger = logger;
23 | _orderService = orderService;
24 | }
25 |
26 | [HttpGet]
27 | public async Task PostOrder(string input)
28 | {
29 | return await _orderService.PostOrder(input);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/ICapitalApi.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using WebApiClientCore;
7 | using WebApiClientCore.Attributes;
8 |
9 | namespace NTccTransaction.Http.Order.WebApi
10 | {
11 | public interface ICapitalApi : IHttpApi
12 | {
13 | [HttpPost("Capital/Record")]
14 | Task Record([JsonContent] TransactionContext transactionContext = null);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/NTccTransaction.Http.Order.WebApi.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 | true
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Program.cs:
--------------------------------------------------------------------------------
1 | using Autofac.Extensions.DependencyInjection;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.Configuration;
4 | using Microsoft.Extensions.Hosting;
5 | using Microsoft.Extensions.Logging;
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq;
9 | using System.Threading.Tasks;
10 |
11 | namespace NTccTransaction.Http.Order.WebApi
12 | {
13 | public class Program
14 | {
15 | public static void Main(string[] args)
16 | {
17 | CreateHostBuilder(args).Build().Run();
18 | }
19 |
20 | public static IHostBuilder CreateHostBuilder(string[] args) =>
21 | Host.CreateDefaultBuilder(args)
22 | .UseServiceProviderFactory(new AutofacServiceProviderFactory())
23 | .ConfigureWebHostDefaults(webBuilder =>
24 | {
25 | webBuilder.UseStartup();
26 | });
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:61695",
8 | "sslPort": 0
9 | }
10 | },
11 | "profiles": {
12 | "IIS Express": {
13 | "commandName": "IISExpress",
14 | "launchBrowser": true,
15 | "launchUrl": "swagger",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "NTccTransaction.Http.Order.WebApi": {
21 | "commandName": "Project",
22 | "dotnetRunMessages": "true",
23 | "launchBrowser": true,
24 | "launchUrl": "swagger",
25 | "applicationUrl": "http://localhost:5000",
26 | "environmentVariables": {
27 | "ASPNETCORE_ENVIRONMENT": "Development"
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Proxy/CapitalProxy.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using WebApiClientCore;
7 |
8 | namespace NTccTransaction.Http.Order.WebApi
9 | {
10 | public interface ICapitalProxy
11 | {
12 | Task Record(TransactionContext transactionContext);
13 |
14 | }
15 |
16 | public class CapitalProxy : ICapitalProxy, INTccTransactionService
17 | {
18 | private readonly ICapitalApi _capitalApi;
19 | public CapitalProxy(ICapitalApi capitalApi)
20 | {
21 | _capitalApi = capitalApi;
22 | }
23 |
24 | [Compensable(CancelMethod = "Record", ConfirmMethod = "Record", Propagation = Propagation.SUPPORTS)]
25 | public async Task Record(TransactionContext transactionContext = null)
26 | {
27 | return await _capitalApi.Record(transactionContext);
28 |
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Services/IOrderService.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 |
7 | namespace NTccTransaction.Http.Order.WebApi
8 | {
9 | public interface IOrderService
10 | {
11 | Task PostOrder(string input, TransactionContext transactionContext = null);
12 |
13 | Task ConfirmOrder(string input, TransactionContext transactionContext = null);
14 |
15 | Task CancelOrder(string input, TransactionContext transactionContext = null);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Services/IOrderService1.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 |
7 | namespace NTccTransaction.Http.Order.WebApi
8 | {
9 | public interface IOrderService1
10 | {
11 | Task PostOrder(string input, TransactionContext transactionContext = null);
12 |
13 | Task ConfirmOrder(string input, TransactionContext transactionContext = null);
14 |
15 | Task CancelOrder(string input, TransactionContext transactionContext = null);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Services/OrderService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Logging;
2 | using NTccTransaction.Abstractions;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 |
8 | namespace NTccTransaction.Http.Order.WebApi
9 | {
10 | public class OrderService : IOrderService, INTccTransactionService
11 | {
12 | private readonly ILogger _logger;
13 | private readonly ICapitalProxy _capitalProxy;
14 | private readonly IOrderService1 _orderService1;
15 |
16 | public OrderService(ILogger logger, ICapitalProxy capitalProxy, IOrderService1 orderService1)
17 | {
18 | _logger = logger;
19 | _capitalProxy = capitalProxy;
20 | _orderService1 = orderService1;
21 | }
22 |
23 | [Compensable(CancelMethod = "CancelOrder", ConfirmMethod = "ConfirmOrder")]
24 | public async Task PostOrder(string input, TransactionContext transactionContext = null)
25 | {
26 | _logger.LogInformation("OrderService.PostOrder:" + input);
27 |
28 | var result = await _capitalProxy.Record(null);
29 | //var result = await _orderService1.PostOrder("vvv");
30 |
31 | if (input.StartsWith("Exception"))
32 | {
33 | throw new Exception("Exception Test");
34 | }
35 | return await Task.FromResult("");
36 | // return await Task.FromResult(result);
37 | }
38 |
39 | public async Task ConfirmOrder(string input, TransactionContext transactionContext = null)
40 | {
41 | _logger.LogInformation("OrderService.ConfirmOrder:" + input);
42 | await Task.CompletedTask;
43 | }
44 |
45 | public async Task CancelOrder(string input, TransactionContext transactionContext = null)
46 | {
47 | _logger.LogInformation("OrderService.CancelOrder:" + input);
48 | await Task.CompletedTask;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Services/OrderService1.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Logging;
2 | using NTccTransaction.Abstractions;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 |
8 | namespace NTccTransaction.Http.Order.WebApi
9 | {
10 | public class OrderService1 : IOrderService1, INTccTransactionService
11 | {
12 | private readonly ILogger _logger;
13 | private readonly ICapitalProxy _capitalProxy;
14 |
15 | public OrderService1(ILogger logger, ICapitalProxy capitalProxy)
16 | {
17 | _logger = logger;
18 | _capitalProxy = capitalProxy;
19 | }
20 |
21 | [Compensable(CancelMethod = "CancelOrder", ConfirmMethod = "ConfirmOrder")]
22 | public async Task PostOrder(string input, TransactionContext transactionContext = null)
23 | {
24 | _logger.LogInformation("OrderService1.PostOrder:" + input);
25 | return await Task.FromResult("1");
26 | }
27 |
28 | public async Task ConfirmOrder(string input, TransactionContext transactionContext = null)
29 | {
30 | _logger.LogInformation("OrderService1.ConfirmOrder:" + input);
31 | await Task.CompletedTask;
32 | }
33 |
34 | public async Task CancelOrder(string input, TransactionContext transactionContext = null)
35 | {
36 | _logger.LogInformation("OrderService1.CancelOrder:" + input);
37 | await Task.CompletedTask;
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/Startup.cs:
--------------------------------------------------------------------------------
1 | using Autofac;
2 | using Autofac.Extensions.DependencyInjection;
3 | using Microsoft.AspNetCore.Builder;
4 | using Microsoft.AspNetCore.Hosting;
5 | using Microsoft.AspNetCore.Mvc;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.DependencyInjection;
8 | using Microsoft.Extensions.Hosting;
9 | using Microsoft.Extensions.Logging;
10 | using Microsoft.OpenApi.Models;
11 | using NTccTransaction.SqlServer;
12 | using NTccTransaction.Aop;
13 | using System;
14 | using System.Collections.Generic;
15 | using System.Linq;
16 | using System.Threading.Tasks;
17 |
18 | namespace NTccTransaction.Http.Order.WebApi
19 | {
20 | public class Startup
21 | {
22 | public Startup(IConfiguration configuration)
23 | {
24 | Configuration = configuration;
25 | }
26 |
27 | public IConfiguration Configuration { get; }
28 |
29 | public ILifetimeScope AutofacContainer { get; private set; }
30 |
31 | public IServiceCollection Services { get; private set; }
32 |
33 |
34 |
35 | // This method gets called by the runtime. Use this method to add services to the container.
36 | public void ConfigureServices(IServiceCollection services)
37 | {
38 |
39 | services.AddControllers();
40 | services.AddSwaggerGen(c =>
41 | {
42 | c.SwaggerDoc("v1", new OpenApiInfo { Title = "NTccTransaction.Http.Order.WebApi", Version = "v1" });
43 | });
44 |
45 | services.AddTransient();
46 | services.AddTransient();
47 |
48 | services.AddTransient();
49 | services.AddTransient();
50 |
51 | services.AddTransient();
52 | services.AddTransient();
53 |
54 | services.AddNTccTransaction((option) =>
55 | {
56 | option.UseSqlServer((sqlOption) =>
57 | {
58 | sqlOption.ConnectionString = Configuration.GetConnectionString("orderDb");
59 |
60 | });
61 |
62 | option.UseCastleInterceptor();
63 | });
64 |
65 | services.AddHttpApi()
66 | .ConfigureHttpApi(Configuration.GetSection(nameof(ICapitalApi)));
67 |
68 | Services = services;
69 | }
70 |
71 | public void ConfigureContainer(ContainerBuilder containerBuilder)
72 | {
73 | containerBuilder.Register(Services);
74 | }
75 |
76 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
77 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
78 | {
79 | loggerFactory.AddLog4Net();
80 |
81 | this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
82 |
83 | if (env.IsDevelopment())
84 | {
85 | app.UseDeveloperExceptionPage();
86 | app.UseSwagger();
87 | app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NTccTransaction.Http.Order.WebApi v1"));
88 | }
89 |
90 | app.UseRouting();
91 |
92 | app.UseAuthorization();
93 |
94 | app.UseEndpoints(endpoints =>
95 | {
96 | endpoints.MapControllers();
97 | });
98 | }
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "ConnectionStrings": {
3 | "orderDb": "Server=127.0.0.1;Database=orderDb;User Id=sa;Password=12345678;"
4 | },
5 | "ICapitalApi": {
6 | "HttpHost": "http://localhost:5001",
7 | "UseParameterPropertyValidate": false,
8 | "UseReturnValuePropertyValidate": false,
9 | "JsonSerializeOptions": {
10 | "IgnoreNullValues": true,
11 | "WriteIndented": false
12 | }
13 | },
14 |
15 | "Logging": {
16 | "LogLevel": {
17 | "Default": "Information",
18 | "Microsoft": "Warning",
19 | "Microsoft.Hosting.Lifetime": "Information"
20 | }
21 | },
22 | "AllowedHosts": "*"
23 | }
24 |
--------------------------------------------------------------------------------
/samples/NTccTransaction.Http.Order.WebApi/log4net.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Check.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | public static class Check
6 | {
7 | public static void NotNull(object value, string name)
8 | {
9 | if(value == null)
10 | {
11 | throw new ArgumentNullException(name);
12 | }
13 | }
14 |
15 | public static void NotNullOrEmpty(string value, string name)
16 | {
17 | if (string.IsNullOrEmpty(value))
18 | {
19 | throw new ArgumentNullException(name);
20 | }
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Exception/CancellingException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.Serialization;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | public class CancellingException : TransactionException
7 | {
8 | public override string Message => $"NTccTransaction,Cancelling exception:{ base.Message}";
9 |
10 | public CancellingException()
11 | : base()
12 | {
13 |
14 | }
15 |
16 | public CancellingException(string message)
17 | : base(message)
18 | {
19 |
20 | }
21 |
22 | public CancellingException(string message, Exception innerException)
23 | : base(message, innerException)
24 | {
25 |
26 | }
27 |
28 | public CancellingException(SerializationInfo info, StreamingContext context)
29 | : base(info, context)
30 | {
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Exception/ConcurrencyTransactionException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | public class ConcurrencyTransactionException : Exception
6 | {
7 | public ConcurrencyTransactionException()
8 | {
9 |
10 | }
11 |
12 | public ConcurrencyTransactionException(string message)
13 | : base(message)
14 | {
15 |
16 | }
17 |
18 | public ConcurrencyTransactionException(string message, Exception innerException)
19 | : base(message, innerException)
20 | {
21 |
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Exception/ConfirmingException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.Serialization;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | public class ConfirmingException: TransactionException
7 | {
8 | public override string Message => $"NTccTransaction,Confirming exception:{ base.Message}";
9 |
10 | public ConfirmingException()
11 | : base()
12 | {
13 |
14 | }
15 |
16 | public ConfirmingException(string message)
17 | : base(message)
18 | {
19 |
20 | }
21 |
22 | public ConfirmingException(string message, Exception innerException)
23 | : base(message, innerException)
24 | {
25 |
26 | }
27 |
28 | public ConfirmingException(SerializationInfo info, StreamingContext context)
29 | : base(info, context)
30 | {
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Exception/NoExistedTransactionException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.Serialization;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | public class NoExistedTransactionException : TransactionException
7 | {
8 | public override string Message => $"NTccTransaction,transaction not existed exception:{ base.Message}";
9 |
10 | public NoExistedTransactionException()
11 | : base()
12 | {
13 |
14 | }
15 |
16 | public NoExistedTransactionException(string message)
17 | : base(message)
18 | {
19 |
20 | }
21 |
22 | public NoExistedTransactionException(string message, Exception innerException)
23 | : base(message, innerException)
24 | {
25 |
26 | }
27 |
28 | public NoExistedTransactionException(SerializationInfo info, StreamingContext context)
29 | : base(info, context)
30 | {
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Exception/TransactionException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.Serialization;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | public class TransactionException: Exception
7 | {
8 | public override string Message => $"NTccTransaction:{ base.Message}";
9 |
10 | public TransactionException()
11 | :base()
12 | {
13 |
14 | }
15 |
16 | public TransactionException(string message)
17 | : base(message)
18 | {
19 |
20 | }
21 |
22 | public TransactionException(string message, Exception innerException)
23 | : base(message, innerException)
24 | {
25 |
26 | }
27 |
28 | public TransactionException(SerializationInfo info, StreamingContext context)
29 | : base(info, context)
30 | {
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Exception/TryingException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.Serialization;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | public class TryingException : TransactionException
7 | {
8 | public override string Message => $"NTccTransaction,Trying exception:{ base.Message}";
9 |
10 | public TryingException()
11 | : base()
12 | {
13 |
14 | }
15 |
16 | public TryingException(string message)
17 | : base(message)
18 | {
19 |
20 | }
21 |
22 | public TryingException(string message, Exception innerException)
23 | : base(message, innerException)
24 | {
25 |
26 | }
27 |
28 | public TryingException(SerializationInfo info, StreamingContext context)
29 | : base(info, context)
30 | {
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/ICompensable.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | public interface ICompensable
6 | {
7 | string ConfirmMethod { get; }
8 |
9 | string CancelMethod { get; }
10 |
11 | Propagation Propagation { get; }
12 |
13 | Type[] DelayCancelExceptionTypes { get; }
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/INTccInterceptor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Threading.Tasks;
5 |
6 | namespace NTccTransaction.Abstractions
7 | {
8 | public interface INTccInterceptor
9 | {
10 | Task InterceptAsync(IMethodInvocation invocation);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/INTccTransactionOptionsExtension.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | ///
6 | /// NTccTransaction options extension
7 | ///
8 | public interface INTccTransactionOptionsExtension
9 | {
10 | ///
11 | /// Registered child service.
12 | ///
13 | /// add service to the
14 | void AddServices(IServiceCollection services);
15 | }
16 | }
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/IUniqueIdentity.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | public interface IUniqueIdentity
4 | {
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/CacheExecutorDescriptor.cs:
--------------------------------------------------------------------------------
1 |
2 |
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Reflection;
6 |
7 | namespace NTccTransaction.Abstractions
8 | {
9 |
10 | ///
11 | /// A descriptor of user definition method.
12 | ///
13 | public class CacheExecutorDescriptor
14 | {
15 | public TypeInfo ServiceTypeInfo { get; set; }
16 |
17 | public MethodInfo MethodInfo { get; set; }
18 |
19 | public TypeInfo ImplTypeInfo { get; set; }
20 |
21 | public ICompensable Attribute { get; set; }
22 |
23 | public IList Parameters { get; set; }
24 | }
25 |
26 | public class ParameterDescriptor
27 | {
28 | public string Name { get; set; }
29 |
30 | public Type ParameterType { get; set; }
31 |
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/IBootstrapper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 |
7 | namespace NTccTransaction.Abstractions
8 | {
9 | public interface IBootstrapper
10 | {
11 | Task BootstrapAsync(CancellationToken stoppingToken);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/IMethodInvocation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NTccTransaction.Abstractions
8 | {
9 | public interface IMethodInvocation
10 | {
11 | object[] Arguments { get; }
12 |
13 | IReadOnlyDictionary ArgumentsDictionary { get; }
14 |
15 | Type[] GenericArguments { get; }
16 |
17 | object TargetObject { get; }
18 |
19 | MethodInfo Method { get; }
20 |
21 | object ReturnValue { get; set; }
22 |
23 | Task ProceedAsync();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/INTccTransactionService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace NTccTransaction.Abstractions
6 | {
7 | public interface INTccTransactionService
8 | {
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/IProcessingServer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace NTccTransaction.Abstractions
6 | {
7 | public interface IProcessingServer : IDisposable
8 | {
9 | void Pulse();
10 |
11 | void Start();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/ITransactionContextEditor.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | public interface ITransactionContextEditor
6 | {
7 | TransactionContext GetContext(object target, MethodBase methodBase, object[] args);
8 |
9 | void SetContext(TransactionContext transactionContext, object target, MethodBase methodBase, object[] args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/ITransactionMethodInvoker.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 | using System.Threading;
6 | using System.Threading.Tasks;
7 |
8 | namespace NTccTransaction.Abstractions
9 | {
10 |
11 | ///
12 | /// Perform user definition method of tcc.
13 | ///
14 | public interface ITransactionMethodInvoker
15 | {
16 | ///
17 | /// Invoke tcc transaction method with TransactionContext and InvocationContext.
18 | ///
19 | /// The transaction context
20 | /// The invocation context
21 | /// The object of .
22 | Task InvokeAsync(TransactionContext transactionContext, InvocationContext invocationContext, CancellationToken cancellationToken = default);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/ITransactionRecoveryProcessingServer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace NTccTransaction.Abstractions
6 | {
7 | public interface ITransactionRecoveryProcessingServer : IProcessingServer
8 | {
9 | bool IsHealthy();
10 |
11 | void ReStart(bool force = false);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/ITransactionServiceSelector.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace NTccTransaction.Abstractions
6 | {
7 | ///
8 | /// Defines an interface for selecting an consumer service method to invoke for the current message.
9 | ///
10 | public interface ITransactionServiceSelector
11 | {
12 | ///
13 | /// Selects a set of candidates for the current message associated with
14 | ///
15 | /// A set of candidates or null.
16 | IReadOnlyList SelectCandidates();
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Internal/InvocationContext.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | ///
7 | /// Invocation context:Used to handle Participant's Confirm and Cancel metadata
8 | ///
9 | [Serializable]
10 | public class InvocationContext
11 | {
12 | public Type TargetType { get; }
13 |
14 | public string MethodName { get; }
15 |
16 | public Type[] Parameters { get; }
17 |
18 | public object[] Arguments { get; }
19 |
20 | public InvocationContext(Type targetType, string methodName, Type[] parameters, object[] arguments)
21 | {
22 | Check.NotNull(targetType, nameof(targetType));
23 | Check.NotNull(methodName, nameof(methodName));
24 |
25 | TargetType = targetType;
26 | MethodName = methodName;
27 | Parameters = parameters ?? new Type[0];
28 | Arguments = arguments ?? new object[0];
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/MethodRole.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | public enum MethodRole : int
4 | {
5 | ROOT = 0,
6 | CONSUMER = 1,
7 | PROVIDER = 2,
8 | NORMAL = 3
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/NTccTransaction.Abstractions.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 |
7 |
8 | bin\$(Configuration)\netstandard2.0\NTccTransaction.Abstractions.xml
9 | 1701;1702;1705;CS1591
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/IAmbientTransaction.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | ///
4 | /// Ambient transaction
5 | ///
6 | public interface IAmbientTransaction : ITransactionAccessor
7 | {
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/IParticipant.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace NTccTransaction.Abstractions
9 | {
10 | public interface IParticipant
11 | {
12 | TransactionXid Xid { get; set; }
13 |
14 | InvocationContext ConfirmInvocationContext { get; set; }
15 |
16 | InvocationContext CancelInvocationContext { get; set; }
17 |
18 |
19 | Task CommitAsync(IServiceScopeFactory serviceScopeFactory);
20 | Task RollbackAsync(IServiceScopeFactory serviceScopeFactory);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/ITransaction.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Threading.Tasks;
5 |
6 | namespace NTccTransaction.Abstractions
7 | {
8 | public interface ITransaction : IDisposable
9 | {
10 | event EventHandler Disoped;
11 |
12 | TransactionXid Xid { get; }
13 | TransactionStatus Status { get; set; }
14 | TransactionType TransactionType { get; }
15 | int RetriedCount { get; set; }
16 | DateTime CreateUtcTime { get; }
17 | DateTime LastUpdateUtcTime { get; set; }
18 | int Version { get; set; }
19 |
20 | Task CommitAsync(IServiceScopeFactory serviceScopeFactory);
21 |
22 | Task RollbackAsync(IServiceScopeFactory serviceScopeFactory);
23 |
24 | void AddRetriedCount();
25 |
26 | void AddVersion();
27 |
28 | void ChangeStatus(TransactionStatus status);
29 |
30 |
31 |
32 | #region Participant
33 |
34 | void AddParticipant(IParticipant participant);
35 |
36 | IReadOnlyList FindAllParticipantAsReadOnly();
37 |
38 | #endregion
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/ITransactionAccessor.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | ///
4 | /// Transaction accessor
5 | ///
6 | public interface ITransactionAccessor
7 | {
8 | ITransaction Transaction { get; }
9 |
10 | ///
11 | /// Register transaction
12 | ///
13 | /// The transaction need to be register
14 | void RegisterTransaction(ITransaction transaction);
15 |
16 | ///
17 | /// Unregister transaction
18 | ///
19 | /// The transaction that has been unregister
20 | ITransaction UnRegisterTransaction();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/ITransactionManager.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 | using System.Threading.Tasks;
5 |
6 | namespace NTccTransaction.Abstractions
7 | {
8 | ///
9 | /// Transaction manager
10 | /// Start root transaction
11 | /// Propagate branch transaction
12 | /// Manage transaction lifetime
13 | ///
14 | public interface ITransactionManager
15 | {
16 | ///
17 | /// Get current transaction
18 | ///
19 | ITransaction Current { get; }
20 |
21 | ///
22 | /// Begin transaction
23 | ///
24 | ///
25 | ITransaction Begin();
26 |
27 | ///
28 | /// Begin transaction
29 | ///
30 | ///
31 | ///
32 | ITransaction Begin(object uniqueIdentity);
33 |
34 | ///
35 | /// Propagate branch transaction
36 | ///
37 | ///
38 | ///
39 | ITransaction PropagationNewBegin(TransactionContext transactionContext);
40 |
41 | ///
42 | /// Propagate existing branch transaction
43 | ///
44 | ///
45 | ///
46 | ITransaction PropagationExistBegin(TransactionContext transactionContext);
47 |
48 | ///
49 | /// Has already has transaction
50 | ///
51 | ///
52 | bool IsTransactionActive();
53 |
54 | ///
55 | /// Commit transaction
56 | ///
57 | Task CommitAsync();
58 |
59 | ///
60 | /// Rollback transaction
61 | ///
62 | Task RollbackAsync();
63 |
64 | ///
65 | /// Add participant
66 | ///
67 | ///
68 | void AddParticipant(IParticipant participant);
69 |
70 | ///
71 | ///
72 | ///
73 | ///
74 | ///
75 | ///
76 | bool IsDelayCancelException(Exception tryingException, HashSet allDelayCancelExceptionTypes);
77 |
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/ITransactionRecovery.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Threading.Tasks;
5 |
6 | namespace NTccTransaction.Abstractions
7 | {
8 | public interface ITransactionRecovery
9 | {
10 | Task StartRecoverAsync();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Persistence/ITransactionRepository.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace NTccTransaction.Abstractions
5 | {
6 | public interface ITransactionRepository
7 | {
8 | void Create(ITransaction transaction);
9 |
10 | void Update(ITransaction transaction);
11 |
12 | void Delete(ITransaction transaction);
13 |
14 | ITransaction FindByXid(TransactionXid xid);
15 |
16 | IEnumerable FindAllUnmodifiedSince(DateTime dateTime);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Propagation.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | public enum Propagation:int
4 | {
5 | REQUIRED = 0,
6 |
7 | SUPPORTS = 1,
8 |
9 | MANDATORY = 2,
10 |
11 | REQUIRES_NEW = 3
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/Serialization/ISerializer.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | public interface ISerializer
4 | {
5 | T Deserialize(string content);
6 |
7 | string Serialize(T obj);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/System/Collections/Generic/HashSetExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace System.Collections.Generic
2 | {
3 | public static class HashSetExtensions
4 | {
5 | public static void AddRange(this HashSet set, ICollection items)
6 | {
7 | if(set==null)
8 | {
9 | set = new HashSet();
10 | }
11 |
12 | if(items!=null && items.Count > 0)
13 | {
14 | foreach(var item in items)
15 | {
16 | set.Add(item);
17 | }
18 | }
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/System/Collections/Generic/StackExtension.cs:
--------------------------------------------------------------------------------
1 | namespace System.Collections.Generic
2 | {
3 | public static class StackExtension
4 | {
5 | public static bool IsNull(this Stack stack)
6 | {
7 | return stack == null ;
8 | }
9 |
10 | public static bool IsNullOrEmpty(this Stack stack)
11 | {
12 | return stack == null || stack.Count <= 0;
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/System/Reflection/MethodBaseExtensions.cs:
--------------------------------------------------------------------------------
1 | using NTccTransaction.Abstractions;
2 | using System.Linq;
3 |
4 | namespace System.Reflection
5 | {
6 | public static class MethodBaseExtensions
7 | {
8 | public static Type[] GetParameterTypes(this MethodBase method)
9 | {
10 | Check.NotNull(method, nameof(method));
11 |
12 | return method.GetParameters().Select(p => p.ParameterType).ToArray();
13 | }
14 |
15 | public static int GetParameterPosition(this MethodBase methodInfo)
16 | {
17 | var parameterTypes = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();
18 | for (int i = 0; i < parameterTypes.Length; i++)
19 | {
20 | if (parameterTypes[i] == typeof(T))
21 | {
22 | return i;
23 | }
24 | }
25 |
26 | return -1;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/TransactionContext.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | [Serializable]
6 | public class TransactionContext
7 | {
8 | public TransactionXid Xid { get; set; }
9 |
10 | public TransactionStatus Status { get; set; }
11 |
12 | public TransactionContext()
13 | {
14 |
15 | }
16 |
17 | public TransactionContext(TransactionXid xid, TransactionStatus status)
18 | {
19 | Xid = xid?? throw new ArgumentNullException(nameof(xid));
20 | Status = status;
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/TransactionStatus.cs:
--------------------------------------------------------------------------------
1 | namespace NTccTransaction.Abstractions
2 | {
3 | ///
4 | /// Transaction status
5 | ///
6 | public enum TransactionStatus: int
7 | {
8 | TRYING=0,
9 |
10 | CONFIRMING=1,
11 |
12 | CANCELLING = 2
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/TransactionType.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel;
2 |
3 | namespace NTccTransaction.Abstractions
4 | {
5 | ///
6 | /// Transaction type
7 | ///
8 | public enum TransactionType:int
9 | {
10 | [Description("Root transaction")]
11 | ROOT=1,
12 |
13 | [Description("Branch transaction")]
14 | BRANCH = 2
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Abstractions/TransactionXId.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Text;
4 |
5 | namespace NTccTransaction.Abstractions
6 | {
7 | [Serializable]
8 | public class TransactionXid
9 | {
10 | [field: NonSerialized]
11 | private static string CUSTOMIZED_TRANSACTION_ID = "UniqueIdentity";
12 |
13 | public int FormatId { get; set; } = 1;
14 | public string GlobalTransactionId { get; set; }
15 | public string BranchQualifier { get; set; }
16 |
17 | public TransactionXid()
18 | {
19 | GlobalTransactionId = Guid.NewGuid().ToString();
20 | BranchQualifier = Guid.NewGuid().ToString();
21 | }
22 |
23 | public TransactionXid(object uniqueIdentity)
24 | {
25 | if (uniqueIdentity == null)
26 | {
27 |
28 | GlobalTransactionId = Guid.NewGuid().ToString();
29 | BranchQualifier = Guid.NewGuid().ToString();
30 | }
31 | else
32 | {
33 | this.GlobalTransactionId = CUSTOMIZED_TRANSACTION_ID;
34 | this.BranchQualifier = uniqueIdentity.ToString();
35 | }
36 | }
37 |
38 |
39 | public TransactionXid(string globalTransactionId)
40 | {
41 | this.GlobalTransactionId = globalTransactionId;
42 | this.BranchQualifier = Guid.NewGuid().ToString();
43 | }
44 |
45 |
46 | public TransactionXid(string globalTransactionId, string branchQualifier)
47 | {
48 | this.GlobalTransactionId = globalTransactionId;
49 | this.BranchQualifier = branchQualifier;
50 | }
51 |
52 | public TransactionXid Clone()
53 | {
54 | string cloneGlobalTransactionId = null;
55 | string cloneBranchQualifier = null;
56 |
57 | if (!string.IsNullOrEmpty(this.GlobalTransactionId))
58 | {
59 | cloneGlobalTransactionId = this.GlobalTransactionId.Clone().ToString();
60 | }
61 |
62 | if (!string.IsNullOrEmpty(this.BranchQualifier))
63 | {
64 | cloneBranchQualifier = this.BranchQualifier.Clone().ToString();
65 | }
66 |
67 | return new TransactionXid(cloneGlobalTransactionId, cloneBranchQualifier);
68 | }
69 |
70 | public override string ToString()
71 | {
72 | var stringBuilder = new StringBuilder();
73 |
74 | if (CUSTOMIZED_TRANSACTION_ID.SequenceEqual(this.GlobalTransactionId))
75 | {
76 | // format:UniqueIdentity:xxxx-xxxx-xxxx-xxxx
77 | stringBuilder.Append(this.GlobalTransactionId.ToString());
78 | stringBuilder.Append(":").Append(this.BranchQualifier);
79 |
80 | }
81 | else
82 | {
83 | // format:xxxx-xxxx-xxxx-xxxx:xxxx-xxxx-xxxx-xxxx
84 | stringBuilder.Append(new Guid(this.GlobalTransactionId));
85 | stringBuilder.Append(":").Append(new Guid(this.BranchQualifier));
86 | }
87 |
88 | return stringBuilder.ToString();
89 | }
90 |
91 | public override int GetHashCode()
92 | {
93 | const int prime = 31;
94 | int result = 1;
95 | result = prime * result + this.FormatId;
96 | result = prime * result + this.BranchQualifier.GetHashCode();
97 | result = prime * result + this.GlobalTransactionId.GetHashCode();
98 | return result;
99 | }
100 |
101 | public override bool Equals(object obj)
102 | {
103 | if (object.ReferenceEquals(this, obj))
104 | {
105 | return true;
106 | }
107 | else if (obj == null)
108 | {
109 | return false;
110 | }
111 | else if (this.GetType() != obj.GetType())
112 | {
113 | return false;
114 | }
115 | TransactionXid other = (TransactionXid)obj;
116 | if (this.FormatId != other.FormatId)
117 | {
118 | return false;
119 | }
120 | else if (!this.BranchQualifier.SequenceEqual(other.BranchQualifier))
121 | {
122 | return false;
123 | }
124 | else if (!this.GlobalTransactionId.SequenceEqual(other.GlobalTransactionId))
125 | {
126 | return false;
127 | }
128 | return true;
129 | }
130 |
131 | public static bool operator ==(TransactionXid left, TransactionXid right)
132 | {
133 | return left.Equals(right);
134 | }
135 |
136 | public static bool operator !=(TransactionXid left, TransactionXid right)
137 | {
138 | return !(left == right);
139 | }
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/NTccTransaction.Aop/AutofacRegistration.cs:
--------------------------------------------------------------------------------
1 | using Autofac;
2 | using Autofac.Builder;
3 | using Autofac.Core;
4 | using Autofac.Extras.DynamicProxy;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using NTccTransaction.Abstractions;
7 | using System;
8 | using System.Collections.Generic;
9 | using System.Linq;
10 | using System.Reflection;
11 | using System.Text;
12 |
13 | namespace NTccTransaction.Aop
14 | {
15 | public static class AutofacRegistration
16 | {
17 | public static void Register(this ContainerBuilder builder, IServiceCollection services)
18 | {
19 |
20 | foreach (var service in services)
21 | {
22 | if (service.ImplementationType != null && typeof(INTccTransactionService).IsAssignableFrom(service.ImplementationType))
23 | {
24 | // Test if the an open generic type is being registered
25 | var serviceTypeInfo = service.ServiceType.GetTypeInfo();
26 | if (serviceTypeInfo.IsGenericTypeDefinition)
27 | {
28 | builder
29 | .RegisterGeneric(service.ImplementationType)
30 | .As(service.ServiceType)
31 | .ConfigureLifecycle(service.Lifetime)
32 | .ConfigureConventions();
33 | }
34 | else
35 | {
36 | builder
37 | .RegisterType(service.ImplementationType)
38 | .As(service.ServiceType)
39 | .ConfigureLifecycle(service.Lifetime)
40 | .ConfigureConventions();
41 | }
42 | }
43 |
44 | }
45 | }
46 |
47 | public static IRegistrationBuilder ConfigureConventions(
48 | this IRegistrationBuilder registrationBuilder)
49 | where TActivatorData : ReflectionActivatorData
50 | {
51 | var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType;
52 | if (serviceType == null)
53 | {
54 | return registrationBuilder;
55 | }
56 |
57 | var implementationType = registrationBuilder.ActivatorData.ImplementationType;
58 | if (implementationType == null)
59 | {
60 | return registrationBuilder;
61 | }
62 |
63 | if (serviceType.IsInterface)
64 | {
65 | registrationBuilder = registrationBuilder.EnableInterfaceInterceptors();
66 | }
67 |
68 | registrationBuilder.InterceptedBy(typeof(TccAsyncDeterminationInterceptor<>).MakeGenericType(typeof(CompensableTransactionInterceptor)));
69 | registrationBuilder.InterceptedBy(typeof(TccAsyncDeterminationInterceptor<>).MakeGenericType(typeof(ResourceCoordinatorInterceptor)));
70 |
71 | return registrationBuilder;
72 | }
73 |
74 | ///
75 | /// Configures the lifecycle on a service registration.
76 | ///
77 | /// The activator data type.
78 | /// The object registration style.
79 | /// The registration being built.
80 | /// The lifecycle specified on the service registration.
81 | ///
82 | /// The , configured with the proper lifetime scope,
83 | /// and available for additional configuration.
84 | ///
85 | private static IRegistrationBuilder