Я получаю сообщение об ошибке:
«Область времени жизни запроса не может быть создана, поскольку HttpContext недоступен».
если я попытаюсь настроить свой веб-API.
HttpContext недоступен в System.Web.Http.SelfHost, но есть ли альтернатива?
Пример с моим 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.
Я получил InvalidOperationException с сообщением:
Невозможно создать область времени жизни запроса, так как HttpContext недоступен.
IMyClass регистрируется в global.asax следующим образом:
m_builder.Register<IMyClass>((c, p) =>
{
//...
//return ...
}
В то время как IIS-хостинг работает нормально, но при самостоятельном размещении IoC с AutoFac не работает.