0

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.

user437899
  • 8,879
  • 13
  • 51
  • 71

1 Answers1

0

You are using Autofac's MVC integration package with Web API, while you should really be using Autofac.WebApi http://nuget.org/packages/Autofac.WebApi

You can read more about it here - http://alexmg.com/post/2012/09/01/New-features-in-the-Autofac-MVC-4-and-Web-API-(Beta)-Integrations.aspx

Filip W
  • 27,097
  • 6
  • 95
  • 82