Can HttpContext.Current be null

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Madhur

    Can HttpContext.Current be null

    Hello

    I am delivering an asp.net 2.0 application to my customer. I need to know,
    If I need to check for the condition of HttpContext.Cur rent to be null
    in my business logic. I have extensively used Cache and Session objects, and
    currently I assume HttpContext.Cur rent object to be existent.

    Also, since I do not have this check, some of my unit test cases fail
    because there is no HttpContext for them.

    How do you handle this scenario. ?

    Should I include check for existence of HttpContext.Cur rent and may be even
    more for Session and Cache objects ?

    --
    Madhur


  • bruce barker

    #2
    Re: Can HttpContext.Cur rent be null

    HttpContext is defined by the asp.net request processor. it will be null
    if your classes are not referenced from a web request. it will be null
    in a unit test, for a control loaded in the designer, or if your code
    starts a background thread, it will be null in the new thread.

    -- bruce (sqlwork.com)

    Madhur wrote:
    Hello
    >
    I am delivering an asp.net 2.0 application to my customer. I need to know,
    If I need to check for the condition of HttpContext.Cur rent to be null
    in my business logic. I have extensively used Cache and Session objects, and
    currently I assume HttpContext.Cur rent object to be existent.
    >
    Also, since I do not have this check, some of my unit test cases fail
    because there is no HttpContext for them.
    >
    How do you handle this scenario. ?
    >
    Should I include check for existence of HttpContext.Cur rent and may be even
    more for Session and Cache objects ?
    >
    --
    Madhur
    >
    >

    Comment

    • Madhur

      #3
      Re: Can HttpContext.Cur rent be null

      Hi Bruce

      Thanks for the info.

      Do you have any recommendations on how to go about the unit testing of the
      procedures which make use of
      HttpContext, session or Cache objects.

      Thanks in Advance.

      Madhur

      "bruce barker" <nospam@nospam. comwrote in message
      news:#P$#vRL4IH A.2580@TK2MSFTN GP06.phx.gbl...
      HttpContext is defined by the asp.net request processor. it will be null
      if your classes are not referenced from a web request. it will be null in
      a unit test, for a control loaded in the designer, or if your code starts
      a background thread, it will be null in the new thread.
      >
      -- bruce (sqlwork.com)
      >
      Madhur wrote:
      >Hello
      >>
      >I am delivering an asp.net 2.0 application to my customer. I need to
      >know, If I need to check for the condition of HttpContext.Cur rent to be
      >null
      >in my business logic. I have extensively used Cache and Session objects,
      >and currently I assume HttpContext.Cur rent object to be existent.
      >>
      >Also, since I do not have this check, some of my unit test cases fail
      >because there is no HttpContext for them.
      >>
      >How do you handle this scenario. ?
      >>
      >Should I include check for existence of HttpContext.Cur rent and may be
      >even more for Session and Cache objects ?
      >>
      >--
      >Madhur

      Comment

      • Jeff Winn

        #4
        Re: Can HttpContext.Cur rent be null

        Like Bruce said earlier, HttpContext.Cur rent can be null - however accessing
        the contextual information from an ASP.NET page or user control will most
        likely ensure that it never will be null. HttpContext.Cur rent really depends
        when it's being accessed to determine if it contains any data.

        As for your followup question, just structure your code so it won't rely on
        having contextual information available. As long as the context information
        is only used from your website you shouldn't have any problems writing unit
        tests for your business logic. Also, not requiring the context information
        would allow you to reuse the assemblies (assuming you have your business
        logic separated from your presentation layer) in other applications by
        simply referencing the assembly.

        void DoSomething() {
        int id = (int)HttpContex t.Current.Sessi on["id"];

        // Use the id.
        }

        void DoSomething(int id) {
        // Use the id.
        }

        protected void Page_Load(objec t sender, EventArgs e) {
        this.DoSomethin g((int)this.Ses sion["id"]);
        }

        You can see both methods do the exact same thing, one of them just doesn't
        rely on context information while the other does.

        "Madhur" <sdf@df.comwrot e in message
        news:DE027634-48FC-46AD-B393-F1886EE9AD54@mi crosoft.com...
        Hi Bruce
        >
        Thanks for the info.
        >
        Do you have any recommendations on how to go about the unit testing of the
        procedures which make use of
        HttpContext, session or Cache objects.
        >
        Thanks in Advance.
        >
        Madhur
        >
        "bruce barker" <nospam@nospam. comwrote in message
        news:#P$#vRL4IH A.2580@TK2MSFTN GP06.phx.gbl...
        >HttpContext is defined by the asp.net request processor. it will be null
        >if your classes are not referenced from a web request. it will be null in
        >a unit test, for a control loaded in the designer, or if your code starts
        >a background thread, it will be null in the new thread.
        >>
        >-- bruce (sqlwork.com)
        >>
        >Madhur wrote:
        >>Hello
        >>>
        >>I am delivering an asp.net 2.0 application to my customer. I need to
        >>know, If I need to check for the condition of HttpContext.Cur rent to be
        >>null
        >>in my business logic. I have extensively used Cache and Session objects,
        >>and currently I assume HttpContext.Cur rent object to be existent.
        >>>
        >>Also, since I do not have this check, some of my unit test cases fail
        >>because there is no HttpContext for them.
        >>>
        >>How do you handle this scenario. ?
        >>>
        >>Should I include check for existence of HttpContext.Cur rent and may be
        >>even more for Session and Cache objects ?
        >>>
        >>--
        >>Madhur
        >

        Comment

        Working...