Skip to content

Commit 6e85aa1

Browse files
authored
Dispose once (#84)
* Dispose only once * update
1 parent cba251c commit 6e85aa1

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/GraphQL.AspNetCore3/GraphQLHttpMiddleware.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,13 @@ protected virtual async Task HandleBatchRequestAsync(
571571
protected virtual async Task<ExecutionResult> ExecuteScopedRequestAsync(HttpContext context, GraphQLRequest? request, IDictionary<string, object?> userContext)
572572
{
573573
var scope = _serviceScopeFactory.CreateScope();
574-
if (scope is IAsyncDisposable ad) {
575-
try {
576-
return await ExecuteRequestAsync(context, request, scope.ServiceProvider, userContext);
577-
} finally {
578-
await ad.DisposeAsync().ConfigureAwait(false);
579-
}
580-
} else {
581-
using (scope)
582-
return await ExecuteRequestAsync(context, request, scope.ServiceProvider, userContext);
574+
try {
575+
return await ExecuteRequestAsync(context, request, scope.ServiceProvider, userContext);
576+
} finally {
577+
if (scope is IAsyncDisposable ad)
578+
await ad.DisposeAsync();
579+
else
580+
scope.Dispose();
583581
}
584582
}
585583

src/GraphQL.AspNetCore3/WebSockets/GraphQLWs/SubscriptionServer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ protected override async Task<ExecutionResult> ExecuteRequestAsync(OperationMess
283283
var request = Serializer.ReadNode<GraphQLRequest>(message.Payload)
284284
?? throw new ArgumentNullException(nameof(message) + "." + nameof(OperationMessage.Payload));
285285
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
286-
using var scope = ServiceScopeFactory.CreateScope();
286+
var scope = ServiceScopeFactory.CreateScope();
287287
try {
288288
var options = new ExecutionOptions {
289289
Query = request.Query,
@@ -301,6 +301,8 @@ protected override async Task<ExecutionResult> ExecuteRequestAsync(OperationMess
301301
} finally {
302302
if (scope is IAsyncDisposable ad)
303303
await ad.DisposeAsync();
304+
else
305+
scope.Dispose();
304306
}
305307
}
306308

src/GraphQL.AspNetCore3/WebSockets/SubscriptionsTransportWs/SubscriptionServer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected override async Task<ExecutionResult> ExecuteRequestAsync(OperationMess
190190
var request = Serializer.ReadNode<GraphQLRequest>(message.Payload)
191191
?? throw new ArgumentNullException(nameof(message) + "." + nameof(OperationMessage.Payload));
192192
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
193-
using var scope = ServiceScopeFactory.CreateScope();
193+
var scope = ServiceScopeFactory.CreateScope();
194194
try {
195195
var options = new ExecutionOptions {
196196
Query = request.Query,
@@ -208,6 +208,8 @@ protected override async Task<ExecutionResult> ExecuteRequestAsync(OperationMess
208208
} finally {
209209
if (scope is IAsyncDisposable ad)
210210
await ad.DisposeAsync();
211+
else
212+
scope.Dispose();
211213
}
212214
}
213215

0 commit comments

Comments
 (0)