[英]HttpSelfHostServer and HttpContext.Current
I'm working on a self-hosted ASP.NET web api-application. 我正在开发一个自托管的ASP.NET web api-application。 Everything works fine, but now I'm struggling with HttpContext
: 一切正常,但现在我正在努力与HttpContext
:
I need to save session-informations from the client. 我需要从客户端保存会话信息。 But HttpContext.Current
is always null. 但是HttpContext.Current
总是为null。 So it's obvious that my HttpSelfHostServer
don't work with the static HttpContext-Class. 所以很明显我的HttpSelfHostServer
不能使用静态HttpContext-Class。
The thing I don't understand is: why..? 我不明白的是:为什么......? And I can't figure out a way to tell neither HtttpSelfHostServer
nor HttpSelfHostConfiguration
to work with HttpContext
. 我无法想办法告诉HtttpSelfHostServer
和HttpSelfHostConfiguration
都不能使用HttpContext
。
Here's what I'm doing: 这是我正在做的事情:
Creating a HttpSelfHostConfiguration
创建HttpSelfHostConfiguration
Service-Resolvers
& Routes
1.1添加Service-Resolvers
和Routes
UserNamePassword-Validator
1.2添加自定义UserNamePassword-Validator
create new Instance of HttpSelfHostServer with the config 使用config创建新的HttpSelfHostServer实例
server.OpenAsync().Wait()
2.1 server.OpenAsync().Wait()
Any help how I can tell my server to work with HttpContext.Current
is greatly appreciated! 任何帮助我如何告诉我的服务器使用HttpContext.Current
非常感谢! Cheers! 干杯!
You won't be able to use HttpContext in a self-hosted environment. 您将无法在自托管环境中使用HttpContext。 HttpContext is set by the ASP.Net pipeline, which you won't have if you don't run under IIS/ASP.Net. HttpContext由ASP.Net管道设置,如果不在IIS / ASP.Net下运行,则不会有。
The HttpContext is only available in the Web-Hosting mode, in which the HttpControllerHandler creates the request. HttpContext仅在Web-Hosting模式下可用,HttpControllerHandler在其中创建请求。
FYI- I invite you to read the following great articles from Pedro Felix to better understand the different hosting models: 仅供参考 - 我邀请您阅读Pedro Felix的以下精彩文章,以便更好地了解不同的托管模式:
To get around this problem (I find I am using a lot of components these days that need to work equally well in Web API and MVC), you can try this old shim I wrote to give you back an HttpContext-like interface that works in both flavours. 为了解决这个问题(我发现我现在使用很多组件需要在Web API和MVC中同样运行良好),你可以试试我写的这个旧的shim给你一个类似HttpContext的界面。两种口味。 It's on NuGet also, here's the source: Link on github (or Link on Nuget ) 它也在NuGet上,这里是源代码: github 上的 链接 (或Nuget上的链接 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.