Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

c# - HttpSelfHostServer and HttpContext.Current

I'm working on a self-hosted ASP.NET web api-application. Everything works fine, but now I'm struggling with HttpContext:

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.

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.

Here's what I'm doing:

  1. Creating a HttpSelfHostConfiguration

    • 1.1 Adding Service-Resolvers & Routes
    • 1.2 Adding custom UserNamePassword-Validator
  2. create new Instance of HttpSelfHostServer with the config

    • 2.1 server.OpenAsync().Wait()

Any help how I can tell my server to work with HttpContext.Current is greatly appreciated! Cheers!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

The HttpContext is only available in the Web-Hosting mode, in which the HttpControllerHandler creates the request.

FYI- I invite you to read the following great articles from Pedro Felix to better understand the different hosting models:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...