Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/JsonApiDotNetCore/Services/ScopedServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using JsonApiDotNetCore.Internal;
using Microsoft.AspNetCore.Http;
using System;

Expand All @@ -23,6 +24,16 @@ public RequestScopedServiceProvider(IHttpContextAccessor httpContextAccessor)
}

/// <inheritdoc />
public object GetService(Type serviceType) => _httpContextAccessor.HttpContext.RequestServices.GetService(serviceType);
public object GetService(Type serviceType)
{
if (_httpContextAccessor.HttpContext == null)
throw new JsonApiException(500,
"Cannot resolve scoped service outside the context of an HTTP Request.",
detail: "If you are hitting this error in automated tests, you should instead inject your own "
+ "IScopedServiceProvider implementation. See the GitHub repository for how we do this internally. "
+ "https://github.com/json-api-dotnet/JsonApiDotNetCore/search?q=TestScopedServiceProvider&unscoped_q=TestScopedServiceProvider");

return _httpContextAccessor.HttpContext.RequestServices.GetService(serviceType);
}
}
}