Skip to content

Commit 5fedd02

Browse files
Add new GetScope() overload (#85)
--------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5b0ef94 commit 5fedd02

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/Immediate.Cache.Shared/ApplicationCacheBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ private async Task RunHandler(CancellationTokenSource tokenSource)
195195
try
196196
{
197197
var token = tokenSource.Token;
198-
var scope = handler.GetScope();
198+
var scope = handler.GetScope(out var service);
199199

200200
await using (scope.ConfigureAwait(false))
201201
{
202-
var response = await scope.Service
202+
var response = await service
203203
.HandleAsync(
204204
request,
205205
token

src/Immediate.Cache.Shared/Owned.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,30 @@ IServiceScopeFactory serviceScopeFactory
2222
/// An <see cref="OwnedScope{T}"/> containing both the scope and the service, so that the scope can be disposed
2323
/// at the appropriate time.
2424
/// </returns>
25-
public OwnedScope<T> GetScope()
25+
public OwnedScope<T> GetScope() => GetScope(out _);
26+
27+
/// <summary>
28+
/// Creates a temporary scope and gets an instance of the service from that scope.
29+
/// </summary>
30+
/// <param name="service">
31+
/// The instance of the service created from the scope.
32+
/// </param>
33+
/// <returns>
34+
/// An <see cref="OwnedScope{T}"/> containing both the scope and the service, so that the scope can be disposed
35+
/// at the appropriate time.
36+
/// </returns>
37+
public OwnedScope<T> GetScope(out T service)
2638
{
2739
var scope = serviceScopeFactory.CreateAsyncScope();
28-
return new(
29-
scope.ServiceProvider.GetRequiredService<T>(),
30-
scope
31-
);
40+
try
41+
{
42+
service = scope.ServiceProvider.GetRequiredService<T>();
43+
return new(service, scope);
44+
}
45+
catch
46+
{
47+
scope.Dispose();
48+
throw;
49+
}
3250
}
3351
}

0 commit comments

Comments
 (0)