エラーが発生します:
「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 が失敗します。