HttpCOntext is null for windows application
-
Hi I am developing an windows application that tries access our handler which calls a provider for the configuration settings. Our Provider cs file contains constructors that access the current httpcontext which is null always, if i access it from my application. My colleague suggested to find a way to instantiate that HttpContext. I googles it but could find any thing. Does anyone here have any idea please!! Any Ideas would be great! Thanks in advancs,
-
Hi I am developing an windows application that tries access our handler which calls a provider for the configuration settings. Our Provider cs file contains constructors that access the current httpcontext which is null always, if i access it from my application. My colleague suggested to find a way to instantiate that HttpContext. I googles it but could find any thing. Does anyone here have any idea please!! Any Ideas would be great! Thanks in advancs,
Hi, i'm just guessing but I don't think it is possible to have an HTTP context within an windows application. How do you use the HttpContext? Maybe it is easy to replace this one against sth that is useful within windows apps and web apps. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
Hi, i'm just guessing but I don't think it is possible to have an HTTP context within an windows application. How do you use the HttpContext? Maybe it is easy to replace this one against sth that is useful within windows apps and web apps. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
Hi Sebastian, Thank you for your reply. I didn't get exactly. The followign code explains clearly how I want to have it in windows applciation. My dll which access my handler. private void button1_Click(object sender, EventArgs e) { LoadConfigFile(System.Threading.Thread.GetDomain().BaseDirectory); HttpContext context; try { context = (HttpContext)HttpContext.Current; } catch { } doXtopNotifications.EmailNotifications emailNotifications = new doXtopNotifications.EmailNotifications(this.lastExecutionTime, this.actions, context); } EmailNotifications.cs public GlobalSettings gs; public void Init(HttpContext ctx) { ConfigurationProvider configProvider = new ConfigurationProvider(ctx); this.gs = configProvider.GlobalSettings; } public EmailNotifications(DateTime lastExecTime, XmlNodeList actions, HttpContext ctx) { Init(ctx); } My Handler class: public class ConfigurationProvider { GlobalSettings _globalSettings = null; HttpContext context = null; public ConfigurationProvider() { context = HttpContext.Current; } public ConfigurationProvider(HttpContext ctx) { context = ctx; } public GlobalSettings GlobalSettings { get { //My Code fails in this block of code. I need to get GlobalSettings to access. if (this.context != null) { if (this.context.Application["GlobalSettings"] == null) loadSettings(); else _globalSettings = (GlobalSettings)this.context.Application["GlobalSettings"]; } return _globalSettings; } } }
-
Hi Sebastian, Thank you for your reply. I didn't get exactly. The followign code explains clearly how I want to have it in windows applciation. My dll which access my handler. private void button1_Click(object sender, EventArgs e) { LoadConfigFile(System.Threading.Thread.GetDomain().BaseDirectory); HttpContext context; try { context = (HttpContext)HttpContext.Current; } catch { } doXtopNotifications.EmailNotifications emailNotifications = new doXtopNotifications.EmailNotifications(this.lastExecutionTime, this.actions, context); } EmailNotifications.cs public GlobalSettings gs; public void Init(HttpContext ctx) { ConfigurationProvider configProvider = new ConfigurationProvider(ctx); this.gs = configProvider.GlobalSettings; } public EmailNotifications(DateTime lastExecTime, XmlNodeList actions, HttpContext ctx) { Init(ctx); } My Handler class: public class ConfigurationProvider { GlobalSettings _globalSettings = null; HttpContext context = null; public ConfigurationProvider() { context = HttpContext.Current; } public ConfigurationProvider(HttpContext ctx) { context = ctx; } public GlobalSettings GlobalSettings { get { //My Code fails in this block of code. I need to get GlobalSettings to access. if (this.context != null) { if (this.context.Application["GlobalSettings"] == null) loadSettings(); else _globalSettings = (GlobalSettings)this.context.Application["GlobalSettings"]; } return _globalSettings; } } }
Well, as I can see it you are using the context to get the "GlobalSettings" from the Application-wide cache (ApplicationState). As a windows app itself does not use the same "session-model" like webapps, you have to load the settings when you want them to access. I propose a little refactoring of your code. Maybe the "loadSettings"-method should return an instance of "GlobalSettings". If you have an instance of HttpContext, store the settings within the context. This will work for your webapps. If there is no HttpContext you are propably within a windows app, so you don't store them within a context, just return them. (If you like to cache them, just use the singleton-pattern). Hope this helps you a bit. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
Well, as I can see it you are using the context to get the "GlobalSettings" from the Application-wide cache (ApplicationState). As a windows app itself does not use the same "session-model" like webapps, you have to load the settings when you want them to access. I propose a little refactoring of your code. Maybe the "loadSettings"-method should return an instance of "GlobalSettings". If you have an instance of HttpContext, store the settings within the context. This will work for your webapps. If there is no HttpContext you are propably within a windows app, so you don't store them within a context, just return them. (If you like to cache them, just use the singleton-pattern). Hope this helps you a bit. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
Thanks again for the reply. Can you post sample code how it be done for the windows application.
-
Thanks again for the reply. Can you post sample code how it be done for the windows application.
1. Change the signature of your loadSettings-method
public GlobalSettings LoadSettings() {
// load the settings (but don't store them in the context)
// return loaded settings
}2. Then change your GlobalSettings-property in ConfigurationProvider
public GlobalSettings GlobalSettings {
get {
GlobalSettings oSettings = null;
// check for context
if(this.context != null) {
// load from context ApplicationState
}// check if found if(oSettings == null) { // load oSettings = this.LoadSettings(); // check again for context if(this.context != null) { // save settings in ApplicationState } } // return result return oSettings; }
}
This should work.
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.