2016-02-11 94 views
2

這是老實說,我無法讓它工作。 上下文始終爲空,無論它注入何處...注入HttpContext始終爲空

是否必須在Startup.cs中添加上下文的實例?如果是,如何?

發起的呼叫

services.AddTransient<SessionService>(); 
Toast.Configure(app.ApplicationServices.GetRequiredService<SessionService>()); 
_session.Set("foo", "bar"); 

吐司類

public class Toast 
{ 
    private static SessionService _session; 
    public static void Configure(SessionService sessionService) 
    { 
     _session = sessionService; 
     Messages = new List<ToastMessage>(); 
    } 
} 

會話服務

public class SessionService 
{ 
    private readonly IHttpContextAccessor _accessor; 
    public SessionService(IHttpContextAccessor httpContextAccessor) 
    { 
     _accessor = httpContextAccessor; 
    } 

    public void Set(string key, string value) 
    { 
     _accessor.HttpContext.Session.SetString(key, value); 
    } 
} 

個例外

System.NullReferenceException 
Object reference not set to an instance of an object. 

System.Reflection.TargetInvocationException 
Exception has been thrown by the target of an invocation. 

堆棧跟蹤

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
    at EveOps.Services.SessionService.Set(String key, Byte[] value) in C:\Users\youri\Documents\Visual Studio 2015\Projects\EveOps\src\EveOps\Services\SessionService.cs:line 25 
    at EveOps.Toast.set_Messages(List`1 value) in C:\Users\youri\Documents\Visual Studio 2015\Projects\EveOps\src\EveOps\Toast\Toast.cs:line 41 
    at EveOps.Toast.Configure(SessionService sessionService) in C:\Users\youri\Documents\Visual Studio 2015\Projects\EveOps\src\EveOps\Toast\Toast.cs:line 18 
    at EveOps.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in C:\Users\youri\Documents\Visual Studio 2015\Projects\EveOps\src\EveOps\Startup.cs:line 162 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 
    at Microsoft.AspNet.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) 
    at Microsoft.AspNet.Hosting.Startup.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder builder) 
    at Microsoft.AspNet.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) 
    at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication() 
+0

你嘗試通過注射來解決吐司?你能發佈Exception和它的StackTrace嗎? –

+0

@aguafrommars還沒有嘗試過,在OP中編輯了堆棧跟蹤。 – NullBy7e

+5

好吧,這是因爲在啓動時沒有HttpContext,如果你在啓動時調用'Set'方法,你總是會得到一個空的'HttpContext'。我只能在請求期間擁有一個上下文。 –

回答

0

感謝@aguafrommars用於說明明顯;上下文僅適用於請求,儘管我知道這一點,但我對DI和服務的理解還不是很好。

我的問題(僞)解決方案:

App.Use() { 
    Toast.Configure(app.ApplicationServices.GetRequiredService<SessionService>()); 
}