@@ -21,11 +21,19 @@ public QueryExecutor(
2121 IServiceProvider applicationServices ,
2222 QueryDelegate queryDelegate ,
2323 FieldMiddleware fieldMiddleware )
24+ : this ( schema , queryDelegate , fieldMiddleware )
2425 {
25- Schema = schema
26- ?? throw new ArgumentNullException ( nameof ( schema ) ) ;
2726 _applicationServices = applicationServices
2827 ?? throw new ArgumentNullException ( nameof ( applicationServices ) ) ;
28+ }
29+
30+ public QueryExecutor (
31+ ISchema schema ,
32+ QueryDelegate queryDelegate ,
33+ FieldMiddleware fieldMiddleware )
34+ {
35+ Schema = schema
36+ ?? throw new ArgumentNullException ( nameof ( schema ) ) ;
2937 _queryDelegate = queryDelegate
3038 ?? throw new ArgumentNullException ( nameof ( queryDelegate ) ) ;
3139
@@ -92,20 +100,33 @@ private async Task<IExecutionResult> ExecuteMiddlewareAsync(
92100 private IRequestServiceScope CreateServiceScope (
93101 IServiceProvider requestServices )
94102 {
95- IServiceScope serviceScope = _applicationServices . CreateScope ( ) ;
96- IServiceProvider services = requestServices ?? Schema . Services ;
97-
98- if ( services == null )
103+ if ( _applicationServices is null )
99104 {
100105 return new RequestServiceScope (
101- serviceScope . ServiceProvider ,
102- serviceScope ) ;
106+ CreateRequestServices ( requestServices ) ,
107+ Disposable . Instance ) ;
103108 }
109+ else
110+ {
111+ IServiceScope serviceScope = _applicationServices . CreateScope ( ) ;
112+ IServiceProvider services = requestServices ?? Schema . Services ;
113+
114+ if ( services == null )
115+ {
116+ return new RequestServiceScope (
117+ serviceScope . ServiceProvider ,
118+ serviceScope ) ;
119+ }
104120
105- services = serviceScope . ServiceProvider . Include ( services ) ;
106- return new RequestServiceScope ( services , serviceScope ) ;
121+ services = serviceScope . ServiceProvider . Include ( services ) ;
122+ return new RequestServiceScope ( services , serviceScope ) ;
123+ }
107124 }
108125
126+ private IServiceProvider CreateRequestServices (
127+ IServiceProvider requestServices ) =>
128+ requestServices ?? Schema . Services ;
129+
109130 public void Dispose ( )
110131 {
111132 Dispose ( true ) ;
@@ -116,12 +137,24 @@ protected virtual void Dispose(bool disposing)
116137 {
117138 if ( ! _disposed )
118139 {
119- if ( disposing && _applicationServices is IDisposable d )
140+ if ( disposing
141+ && _applicationServices != null
142+ && _applicationServices is IDisposable d )
120143 {
121144 d . Dispose ( ) ;
122145 }
123146 _disposed = true ;
124147 }
125148 }
149+
150+ private class Disposable
151+ : IDisposable
152+ {
153+ public void Dispose ( )
154+ {
155+ }
156+
157+ public static Disposable Instance { get ; } = new Disposable ( ) ;
158+ }
126159 }
127160}
0 commit comments