HttpSelfHostServer 和 HttpContext.Current [英] HttpSelfHostServer and HttpContext.Current
问题描述
我正在开发一个自托管的 ASP.NET Web api 应用程序.一切正常,但现在我正在为 HttpContext
苦苦挣扎:
I'm working on a self-hosted ASP.NET web api-application.
Everything works fine, but now I'm struggling with HttpContext
:
我需要保存来自客户端的会话信息.但是 HttpContext.Current
始终为 null.所以很明显,我的 HttpSelfHostServer
不适用于静态 HttpContext-Class.
I need to save session-informations from the client.
But HttpContext.Current
is always null.
So it's obvious that my HttpSelfHostServer
don't work with the static HttpContext-Class.
我不明白的是:为什么..?而且我想不出一种方法来告诉 HtttpSelfHostServer
和 HttpSelfHostConfiguration
与 HttpContext
一起工作.
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
.
这是我正在做的事情:
创建
HttpSelfHostConfiguration
- 1.1 添加
Service-Resolvers
&路线
- 1.2 添加自定义
UserNamePassword-Validator
- 1.1 Adding
Service-Resolvers
&Routes
- 1.2 Adding custom
UserNamePassword-Validator
使用配置创建 HttpSelfHostServer 的新实例
create new Instance of HttpSelfHostServer with the config
- 2.1
server.OpenAsync().Wait()
非常感谢我如何告诉我的服务器使用 HttpContext.Current
的任何帮助!干杯!
Any help how I can tell my server to work with HttpContext.Current
is greatly appreciated!
Cheers!
推荐答案
您将无法在自托管环境中使用 HttpContext.HttpContext 是由 ASP.Net 管道设置的,如果您不在 IIS/ASP.Net 下运行,您将不会拥有该管道.
You won't be able to use HttpContext in a self-hosted environment. HttpContext is set by the ASP.Net pipeline, which you won't have if you don't run under IIS/ASP.Net.
HttpContext 仅在 Web-Hosting 模式下可用,在该模式下 HttpControllerHandler 创建请求.
The HttpContext is only available in the Web-Hosting mode, in which the HttpControllerHandler creates the request.
仅供参考 - 我邀请您阅读 Pedro Felix 的以下精彩文章,以更好地了解不同的托管模型:
FYI- I invite you to read the following great articles from Pedro Felix to better understand the different hosting models:
这篇关于HttpSelfHostServer 和 HttpContext.Current的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!