I get the error:
"The request lifetime scope cannot be created because the HttpContext is not available."
if I try to setup my web api.
HttpContext is not available in System.Web.Http.SelfHost but is there an alternative?
Example with my AuthenticationHandler:
public class AuthenticationHandler : DelegatingHandler
{
private const string m_AuthenticationScheme = "Basic";
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
AuthenticationHeaderValue authenticationHeader = request.Headers.Authorization; //get the authorization header
if (authenticationHeader != null && authenticationHeader.Scheme == m_AuthenticationScheme)
{
Credentials credentials = authenticationHeader.ParseAuthenticationHeader();
if (credentials != null)
{
IMyClass procadCredentials = DependencyResolver.Current.GetService<IMyClass>(); //thows the InvalidOperationException if I use self-hosting
//tried: "Autofac.Integration.Mvc.AutofacDependencyResolver.Current.RequestLifetimeScope.Resolve<IMyClass>();" too.
I got the InvalidOperationException with the message:
The request lifetime scope cannot be created because the HttpContext is not available.
IMyClass is registeres in global.asax like this:
m_builder.Register<IMyClass>((c, p) =>
{
//...
//return ...
}
While IIS-Hosting, it works fine, but using self-hosting, IoC with AutoFac fails.