22using System . Collections . Generic ;
33using System . Threading ;
44using System . Threading . Tasks ;
5+ using Microsoft . Extensions . DependencyInjection ;
56using Shuttle . Core . Contract ;
67using Shuttle . Core . Reflection ;
78
89namespace Shuttle . Core . Threading ;
910
10- public class ProcessorThread : IProcessorThreadContext
11+ public class ProcessorThread
1112{
13+ private readonly IServiceScopeFactory _serviceScopeFactory ;
1214 private readonly CancellationTokenSource _cancellationTokenSource = new ( ) ;
1315 private readonly ProcessorThreadOptions _processorThreadOptions ;
1416
@@ -18,8 +20,9 @@ public class ProcessorThread : IProcessorThreadContext
1820 private bool _started ;
1921 private readonly Thread _thread ;
2022
21- public ProcessorThread ( string name , IProcessor processor , ProcessorThreadOptions processorThreadOptions )
23+ public ProcessorThread ( string name , IServiceScopeFactory serviceScopeFactory , IProcessor processor , ProcessorThreadOptions processorThreadOptions )
2224 {
25+ _serviceScopeFactory = Guard . AgainstNull ( serviceScopeFactory ) ;
2326 Name = Guard . AgainstNull ( name ) ;
2427 Processor = Guard . AgainstNull ( processor ) ;
2528 _processorThreadOptions = Guard . AgainstNull ( processorThreadOptions ) ;
@@ -45,12 +48,7 @@ internal void Deactivate()
4548 _cancellationTokenSource . Cancel ( ) ;
4649 }
4750
48- public object ? GetState ( string key )
49- {
50- Guard . AgainstNullOrEmptyString ( key ) ;
51-
52- return _state . TryGetValue ( key , out var value ) ? value : null ;
53- }
51+ public IState State { get ; } = new State ( ) ;
5452
5553 public event EventHandler < ProcessorThreadExceptionEventArgs > ? ProcessorException ;
5654 public event EventHandler < ProcessorThreadEventArgs > ? ProcessorExecuting ;
@@ -60,11 +58,6 @@ internal void Deactivate()
6058 public event EventHandler < ProcessorThreadEventArgs > ? ProcessorThreadStopped ;
6159 public event EventHandler < ProcessorThreadEventArgs > ? ProcessorThreadStopping ;
6260
63- public void SetState ( string key , object value )
64- {
65- _state [ Guard . AgainstNullOrEmptyString ( key ) ] = value ;
66- }
67-
6861 public async Task StartAsync ( )
6962 {
7063 if ( _started )
@@ -126,7 +119,10 @@ private async void Work()
126119
127120 try
128121 {
129- await Processor . ExecuteAsync ( this , CancellationToken ) ;
122+ using ( var context = new ProcessorThreadContext ( State , _serviceScopeFactory . CreateScope ( ) ) )
123+ {
124+ await Processor . ExecuteAsync ( context , CancellationToken ) ;
125+ }
130126 }
131127 catch ( OperationCanceledException )
132128 {
0 commit comments