我知道错误:
“由于HttpContext不可用,无法创建请求生存期范围。”
如果我试图设置我的web。
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会失败.
发布于 2013-01-28 13:28:09
您正在使用Autofac与Web的MVC集成包,而实际上您应该使用Autofac.WebApi http://nuget.org/packages/Autofac.WebApi
你可以在这里读到更多关于它的信息-- http://alexmg.com/post/2012/09/01/New-features-in-the-Autofac-MVC-4-and-Web-API-(Beta)-Integrations.aspx
https://stackoverflow.com/questions/14561261
复制相似问题