0

エラーが発生します:

「HttpContext が利用できないため、リクエストの有効期間スコープを作成できません。」

Web 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 ホスティング中は正常に動作しますが、セルフホスティングを使用すると、AutoFac を使用した IoC が失敗します。

4

1 に答える 1

0

Web API で Autofac の 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

于 2013-01-28T13:28:09.053 に答える