我有一个netstandard 2.0类库,对于某些功能它需要依赖项注入。我需要在我的库中调用以下方法:
public static class HttpContext
{
/// <summary>
/// Returns the current httpContext
/// </summary>
public static Microsoft.AspNetCore.Http.HttpContext Current => _httpContextAccessor?.HttpContext;
private static Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// Call this in Configure in Startup.cs, to configure the HttpContext.Current.
/// Don't forget to also add 'services.AddHttpContextAccessor();' to ConfigureServices
/// </summary>
/// <param name="app"></param>
public static void Configure(Microsoft.AspNetCore.Builder.IApplicationBuilder app)
{
_httpContextAccessor = app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Http.IHttpContextAccessor>();
}
}
(现在,我知道进入HttpContext是一个拐杖,但是在将我的库转换为netstandard时,这是最好的方法。当然,我很高兴听到关于如何使httpcontext代码可用的另一种建议在.netframework和.netstandard中)
我一直在搜索,但是我找不到如何使它工作的方法。这是可能吗?因为从我发现的内容中,我得到的印象是不支持
Remco