Skip to content

Commit 8a89d76

Browse files
authored
Merge pull request #23 from Cysharp/hotfix/ServiceProviderScope
Create scope service while BatchEngineService is starting.
2 parents c0c77a1 + 71e1e73 commit 8a89d76

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

sandbox/SingleContainedAppWithConfig/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Baz : BatchBase
88
{
99
private readonly IOptions<SingleContainedAppWithConfig.AppConfig> config;
1010
// Batche inject Config on constructor.
11-
public Baz(IOptions<SingleContainedAppWithConfig.AppConfig> config)
11+
public Baz(IOptions<SingleContainedAppWithConfig.AppConfig> config, MyServiceA serviceA, MyServiceB serviceB, MyServiceC serviceC)
1212
{
1313
this.config = config;
1414
}
@@ -25,6 +25,9 @@ public void Hello3()
2525
}
2626
}
2727

28+
public class MyServiceA { }
29+
public class MyServiceB { }
30+
public class MyServiceC { }
2831

2932
namespace SingleContainedAppWithConfig
3033
{
@@ -39,6 +42,10 @@ await MicroBatchFramework.BatchHost.CreateDefaultBuilder()
3942
services.AddOptions();
4043
// mapping json element to class
4144
services.Configure<AppConfig>(hostContext.Configuration.GetSection("AppConfig"));
45+
46+
services.AddScoped<MyServiceA>();
47+
services.AddTransient<MyServiceB>();
48+
services.AddSingleton<MyServiceC>();
4249
})
4350
.RunBatchEngineAsync<Baz>(args);
4451
}

src/MicroBatchFramework/BatchEngineService.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.ExceptionServices;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.Extensions.DependencyInjection;
89

910
namespace MicroBatchFramework
1011
{
@@ -15,7 +16,7 @@ public sealed class BatchEngineService : IHostedService
1516
MethodInfo methodInfo;
1617
IApplicationLifetime appLifetime;
1718
ILogger<BatchEngine> logger;
18-
IServiceProvider provider;
19+
IServiceScope scope;
1920
IBatchInterceptor interceptor;
2021
Task runningTask;
2122
CancellationTokenSource cancellationTokenSource;
@@ -31,23 +32,23 @@ public BatchEngineService(IApplicationLifetime appLifetime, Type type, MethodInf
3132
this.type = type;
3233
this.methodInfo = methodInfo;
3334
this.appLifetime = appLifetime;
34-
this.provider = provider;
35+
this.scope = provider.CreateScope();
3536
this.logger = logger;
3637
this.interceptor = (provider.GetService(typeof(IBatchInterceptor)) as IBatchInterceptor) ?? NullBatchInterceptor.Default;
3738
this.cancellationTokenSource = new CancellationTokenSource();
3839
}
3940

4041
public async Task StartAsync(CancellationToken cancellationToken)
4142
{
42-
await interceptor.OnBatchEngineBeginAsync(provider, logger);
43+
await interceptor.OnBatchEngineBeginAsync(scope.ServiceProvider, logger);
4344

4445
// raise after all event registered
4546
appLifetime.ApplicationStarted.Register(async state =>
4647
{
4748
var self = (BatchEngineService)state;
4849
try
4950
{
50-
var engine = new BatchEngine(self.logger, self.provider, self.interceptor, self.cancellationTokenSource.Token);
51+
var engine = new BatchEngine(self.logger, scope.ServiceProvider, self.interceptor, self.cancellationTokenSource.Token);
5152
if (self.methodInfo != null)
5253
{
5354
self.runningTask = engine.RunAsync(self.type, self.methodInfo, self.args);
@@ -85,6 +86,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
8586
finally
8687
{
8788
await interceptor.OnBatchEngineEndAsync();
89+
scope.Dispose();
8890
}
8991
}
9092
}

0 commit comments

Comments
 (0)