File tree 1 file changed +12
-1
lines changed
src/JsonApiDotNetCore/Services
1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change
1
+ using JsonApiDotNetCore . Internal ;
1
2
using Microsoft . AspNetCore . Http ;
2
3
using System ;
3
4
@@ -23,6 +24,16 @@ public RequestScopedServiceProvider(IHttpContextAccessor httpContextAccessor)
23
24
}
24
25
25
26
/// <inheritdoc />
26
- public object GetService ( Type serviceType ) => _httpContextAccessor . HttpContext . RequestServices . GetService ( serviceType ) ;
27
+ public object GetService ( Type serviceType )
28
+ {
29
+ if ( _httpContextAccessor . HttpContext == null )
30
+ throw new JsonApiException ( 500 ,
31
+ "Cannot resolve scoped service outside the context of an HTTP Request." ,
32
+ detail : "If you are hitting this error in automated tests, you should instead inject your own "
33
+ + "IScopedServiceProvider implementation. See the GitHub repository for how we do this internally. "
34
+ + "https://github.com/json-api-dotnet/JsonApiDotNetCore/search?q=TestScopedServiceProvider&unscoped_q=TestScopedServiceProvider" ) ;
35
+
36
+ return _httpContextAccessor . HttpContext . RequestServices . GetService ( serviceType ) ;
37
+ }
27
38
}
28
39
}
You can’t perform that action at this time.
0 commit comments