Skip to content

fix(#362): improve error message for null HttpContext #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2018
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);
}
}
}