Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. HttpCOntext is null for windows application

HttpCOntext is null for windows application

Scheduled Pinned Locked Moved ASP.NET
workspace
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gottimukkala
    wrote on last edited by
    #1

    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,

    S 1 Reply Last reply
    0
    • G gottimukkala

      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,

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • S SeMartens

        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.

        G Offline
        G Offline
        gottimukkala
        wrote on last edited by
        #3

        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; } } }

        S 1 Reply Last reply
        0
        • G gottimukkala

          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; } } }

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          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.

          G 1 Reply Last reply
          0
          • S SeMartens

            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.

            G Offline
            G Offline
            gottimukkala
            wrote on last edited by
            #5

            Thanks again for the reply. Can you post sample code how it be done for the windows application.

            S 1 Reply Last reply
            0
            • G gottimukkala

              Thanks again for the reply. Can you post sample code how it be done for the windows application.

              S Offline
              S Offline
              SeMartens
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups