44using System ;
55using System . Threading ;
66using System . Threading . Tasks ;
7+ using System . Transactions ;
78
89namespace Winton . Extensions . Threading . Actor
910{
@@ -30,15 +31,15 @@ public static IActor WithStartWork(this IActor self, ActorStartWork work)
3031 /// <param name="self">Actor</param>
3132 /// <param name="work">Work to do.</param>
3233 /// <returns>Actor</returns>
33- public static IActor WithStartWork ( this IActor self , Action work ) => self . WithStartWork ( new ActorStartWork ( work ) ) ;
34+ public static IActor WithStartWork ( this IActor self , Action work ) => self . WithStartWork ( new ActorStartWork ( SuppressTransactionScopeWrapper ( work ) ) ) ;
3435
3536 /// <summary>
3637 /// Specify work for actor start-up.
3738 /// </summary>
3839 /// <param name="self">Actor</param>
3940 /// <param name="work">Async work to do.</param>
4041 /// <returns>Actor</returns>
41- public static IActor WithStartWork ( this IActor self , Func < Task > work ) => self . WithStartWork ( new ActorStartWork ( work ) ) ;
42+ public static IActor WithStartWork ( this IActor self , Func < Task > work ) => self . WithStartWork ( new ActorStartWork ( SuppressTransactionScopeWrapper ( work ) ) ) ;
4243
4344 /// <summary>
4445 /// Specify work for actor start-up.
@@ -52,7 +53,7 @@ public static IActor WithStopWork(this IActor self, ActorStopWork work)
5253 return self ;
5354 }
5455
55- public static IActor WithStopWork ( this IActor self , Action work ) => self . WithStopWork ( new ActorStopWork ( work ) ) ;
56+ public static IActor WithStopWork ( this IActor self , Action work ) => self . WithStopWork ( new ActorStopWork ( SuppressTransactionScopeWrapper ( work ) ) ) ;
5657
5758 /// <summary>
5859 /// Enqueue a procedure.
@@ -184,5 +185,45 @@ public static CancellationToken StoppedToken(this IActor self)
184185
185186 return cancellationTokenSource . Token ;
186187 }
188+
189+ internal static Func < T > SuppressTransactionScopeWrapper < T > ( Func < T > function )
190+ {
191+ return ( ) =>
192+ {
193+ using ( var scope = new TransactionScope (
194+ TransactionScopeOption . Suppress ,
195+ TransactionScopeAsyncFlowOption . Enabled ) )
196+ {
197+ try
198+ {
199+ return function ( ) ;
200+ }
201+ finally
202+ {
203+ scope . Complete ( ) ;
204+ }
205+ }
206+ } ;
207+ }
208+
209+ internal static Action SuppressTransactionScopeWrapper ( Action action )
210+ {
211+ return ( ) =>
212+ {
213+ using ( var scope = new TransactionScope (
214+ TransactionScopeOption . Suppress ,
215+ TransactionScopeAsyncFlowOption . Enabled ) )
216+ {
217+ try
218+ {
219+ action ( ) ;
220+ }
221+ finally
222+ {
223+ scope . Complete ( ) ;
224+ }
225+ }
226+ } ;
227+ }
187228 }
188229}
0 commit comments