External Assembly accesing Session generates an error

Discussion in 'ASP .Net' started by =?Utf-8?B?cmFzMjY=?=, May 16, 2006.

  1. I have a WebSite "App1" which has an external assembly "Lib1".

    Inside this external assembly we have developed some classes that derive
    from "System.Web.UI.Page". One of these pages is called "WebNotification".

    In the WebSite "App1" we create an instance of "WebNotification", but when
    this class tries to access some Session variables it generates the following
    error:

    Session state can only be used when enableSessionState is set to true,
    either in a configuration file or in the Page directive. Please also make
    sure that System.Web.SessionStateModule or a custom session state module is
    included in the <configuration>\<system.web>\<httpModules> section in the
    application configuration.

    I have tried to write "EnableSessionState= true" in the web.config and even
    in the <%page%> directive but the error is still there.

    Why?
    Is there any restriction about accesing Session variables from class defined
    in an external library?
    Is there anyway to avoid this?
     
    =?Utf-8?B?cmFzMjY=?=, May 16, 2006
    #1
    1. Advertising

  2. Since you are most likely instantiating this class in code, you can create a
    constructor or properties and pass the session magic to the page prior to
    displaying it. Treat an external library like a black box and it will be
    your friend.

    You can also go down into the bowels and get the session state object and
    pull that way, provided the class has the proper context at the point you
    are attempting to get session items. You will have to pull session state
    from application, which comes from context, which means you have to be far
    enough into the process to have context for the application, but it should
    be easy enough to do with some digging I do not have any sample code for
    this and I do not consider this the better option.

    I just recently dealt with some code where all sorts of magic was going on,
    including pulling session variables and config items. It was a straight
    library (not inherited from an ASP.NET object), so it was easier to get into
    this madness (no blow ups). It was not discovered until the population of
    variables was moved from config to the database. Since the config element
    was now controlled by user configurable metadata (in the DB), it was removed
    from the config file and boom. A few refactors later and the class no longer
    reached outside of its domain (i.e., it was a black box used by a web app).
    Long term benefit: You can switch to a windows GUI in the future without
    having to tackle the madness.

    Overall, I am not fond of compiled libraries that handle GUI bits, but I can
    see where reusability might dictate this functionality. And, you might have
    to pull things from config or session in these objects, as they cannot move
    to other GUI types (i.e., they are tightly coupled to the web solution, no
    matter what). If, however, you move to a model where there is a GUI facade
    and the GUI bits only handle display (and perhaps basic validation), you
    find there is less need to create pure GUI libraries as the work is all done
    behind the scenes.

    --
    Gregory A. Beamer

    *************************************************
    Think Outside the Box!
    *************************************************
    "ras26" <> wrote in message
    news:...
    >I have a WebSite "App1" which has an external assembly "Lib1".
    >
    > Inside this external assembly we have developed some classes that derive
    > from "System.Web.UI.Page". One of these pages is called "WebNotification".
    >
    > In the WebSite "App1" we create an instance of "WebNotification", but when
    > this class tries to access some Session variables it generates the
    > following
    > error:
    >
    > Session state can only be used when enableSessionState is set to true,
    > either in a configuration file or in the Page directive. Please also make
    > sure that System.Web.SessionStateModule or a custom session state module
    > is
    > included in the <configuration>\<system.web>\<httpModules> section in the
    > application configuration.
    >
    > I have tried to write "EnableSessionState= true" in the web.config and
    > even
    > in the <%page%> directive but the error is still there.
    >
    > Why?
    > Is there any restriction about accesing Session variables from class
    > defined
    > in an external library?
    > Is there anyway to avoid this?
    >
    >
     
    Cowboy \(Gregory A. Beamer\), May 16, 2006
    #2
    1. Advertising

  3. Thanks for your alternative solutuions, but I have already found what was
    wrong.

    We were trying to access the Session variable in the constructor of the
    derived page in the library.

    At this time, Session is not available, so we have move the code to the
    onInit event and all works ok.

    Anyway thanks again for your answer.
     
    =?Utf-8?B?cmFzMjY=?=, May 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prasanna Padmanabhan
    Replies:
    1
    Views:
    833
    Mattias Sjögren
    Nov 19, 2003
  2. Brent
    Replies:
    1
    Views:
    1,389
    Brent
    Jan 23, 2004
  3. SA
    Replies:
    0
    Views:
    493
  4. Horatiu Margavan via .NET 247

    Assembly's manifest definition does not match the assembly reference.

    Horatiu Margavan via .NET 247, Aug 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,653
    Horatiu Margavan via .NET 247
    Aug 30, 2004
  5. Replies:
    0
    Views:
    899
Loading...

Share This Page