How to set the ServiceHostingEnvironmentSection.AspNetCompatibilityEnabled by code in C#
I have two question:
- I have a wcf service hosted in a console application, if i set the ServiceHostingEnvironmentSection.AspNetCompatibilityEnabled to true, can i access HttpContext.Current.Session in the console application.
- If I can a开发者_JAVA技巧ccess the HttpContext.Current.Session in the console application, how to set the ServiceHostingEnvironmentSection.AspNetCompatibilityEnabled by code.
Note that I am using the WSHttpBinding
AFAIK, and put forward in the simplest form:
- no and,
- no, you can't.
To elaborate a little, the ServiceHostingEnvironment
class contains the AspNetCompatibilityEnabled
(see here for details) and is only gettable, not settable.
This would lead one to assume - quite reliably - that you simply can't do it this way. Some guru may be aware of another method, however, I highly doubt it since this is a configuration that I think would need to be evaluated on execution of the application on the server and not during execution.
Compilation (by means of decorating elements with the AspNetCompatibilityRequirements
attribute) can also be used to configure types; but this may well go hand-in-hand with a present config file setting.
For some further reading, check out this article.
I don't think you can.
The AspNetCompatibilityEnabled
setting would let you access the http context of the current application if it is an http wcf web service hosted within IIS. Yours isn't hosted within IIS, as it is a console application.
I don't think it really makes any sense to be able to reach HttpContext.Current.Session
in a console application (as far as I can see, anyway). Whose session (or context) are you trying to reach? You server may have many web sites on it, each of which would have its own context. You console application is not actually related to any of these.
More can be read about this setting about half way down this page.
精彩评论